[Tutor] (no subject)

Mats Wichmann mats at wichmann.us
Fri Feb 28 14:54:34 EST 2020


On 2/28/20 11:57 AM, Mark Lawrence wrote:
> On 28/02/2020 09:02, Abdurauf Isokjonov wrote:
>> Even i’ve installed bs4, python says “no module named bs4”. How to solve
>> this?
> 
> The odds are you've installed beautifulsoup in one version of python but
> are trying to run it from another.  To check try running things like the
> following from the command line:-
> 
> python -c "import bs4"
> python2.7 -c "import bs4"
> python3.6 -c "import bs4"
> python3.7 -c "import bs4"
> 
> What happens?
> 

Indeed, to tie this up a little more with what both Alan and Mark have said:

This has a very simple explanation: it's *always* a problem with the
path, the Python you're running is looking in a set of places, and the
install put it in a place that wasn't in that set. The path your Python
interpreter searches you can get like this:

>>> import sys
>>> print(sys.path)

Here is the most reliable way to make sure you don't get a mismatch -
for the Python you're going to run (note this may happen to be named
python, or python3, or py - the latter if you're using the Python
Launcher on Windows - use the name you're going to use to run it):


python -m pip install --user bs4


we pretty much *never* know that pip, if called as a standalone command,
matches exactly the Python we're going to use.

===

Sadly, this story gets more complicated if you're using an advanced
editor like PyCharm or VS Code or other - they may helpfully try to
construct a virtualenv for you for your project, so you on the command
line don't necessarily *know* which Python to invoke. In that case, you
should install through the environment in the IDE, so it has a chance of
getting it right.

And if you're using Anconda (or Miniconda), that has its own
installation environment that you should make use of (the Conda world
likes to construct virtualenvs too, by the way):

conda install bs4





More information about the Tutor mailing list