Modules are installed using pip on OSX but not found when importing
Asked Answered
A

9

37

I successfully install different modules using pip and they are shown in the

pip list

such as:

beautifulsoup4 (4.4.1)
requests (2.10.0)
Scrapy (1.1.0)

From Terminal

However, whenever I try to import it

import beautifulsoup4 / import bs4 or import Scrapy or import requests

the following error is shown:

$ python
Python 2.7.5 (default, Mar  9 2014, 22:15:05) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named requests

Update: if I launch python when I am at the correct site-packages directory

$ pwd
/usr/local/lib/python2.7/site-packages
$ python
Python 2.7.5 (default, Mar  9 2014, 22:15:05)
>>> import requests
>>> import bs4
>>> import scrapy

Then it works. This would solve the issue if writing directly on the Terminal. However, I have no clue about how to make it work inside a file.py, which will be the normal use.

As far as I know, I only have Python2.7 installed.

From file.py

If I have a file.py saved in some local folder. This contains, for instance

import requests
from bs4 import BeautifulSoup

when I try

python file.py

I get the same error.

Approach

Same happens with any other module from the list. I would think pip is installing them in a directory that Python is not reading, but as per what I read, it is the correct one.

They are all installed here:

/usr/local/lib/python2.7/site-packages

Output requested by Padraic Cunningham:

$ which -a pip
/usr/local/bin/pip
$ which -a python
/usr/bin/python
/usr/local/bin/python

Output requested by leovp:

$ pip -V
pip 8.1.2 from /usr/local/lib/python2.7/site-packages (python 2.7)

Threads already checked

I have checked the following threads, but unfortunately they did not help me to solve the issue:

Any ideas of what the problem is?

Abelmosk answered 20/5, 2016 at 8:23 Comment(11)
You are definitely not using the interpreter that pip is installing for, add the output of which -a pip and which -a pythonEntreat
What does pip -V show? Is there a chance that you have both Python 2 and Python 3 installed?Earleenearlene
@PadraicCunningham I added the output.Abelmosk
OK, now start a shell with /usr/local/bin/python and try importing.Entreat
@Earleenearlene I added also the output you requestedAbelmosk
@PadraicCunningham, this is what I get: $ cd /usr/local/bin/python and -bash: cd: /usr/local/bin/python: Not a directory If I just go one level above, it is OK, and doing ls shows actually inside the list a python directoryAbelmosk
@JoanMM, I meant just type /usr/local/bin/python and hit enter. Your system/default python is /usr/bin/python, pip is installing for /usr/local/bin/python so that is why you cannot seem to import anything you install, when you just type python then the /usr/bin/python interpreter is started .Entreat
@PadraicCunningham, that works. Should I add this somehow in my python.py file?Abelmosk
If you want python to start your /usr/local/bin/python interpreter the simplest way would be to export the path in your .bashrc file.Entreat
@PadraicCunningham, wouldn't it make more sense that pip just installs all modules for /usr/bin/python? Could that be done? Anyhow, if the simplest way is the one you meant, could you explain how to export the path in my .bashrc file?Abelmosk
Simplest solution for that is to download get-pip.py and install it with /usr/bin/python get-pip.py bootstrap.pypa.io/get-pip.pyEntreat
G
10

Since your problem maybe caused due to various reason, I have listed down a few of them here :

The link you were looking for : https://pythonhosted.org/setuptools/setuptools.html#development-mode

  • It may also happen if you have two versions of python installed. If the pip that you are accessing is of one version & the python interpreter used is another.

So just see to that you are using the same version of python to install and use the package.

You may fix this using alias,

First, set up a shell alias:

alias python=/usr/local/bin/python3

Then, type that at a prompt, or put it in your ~/.bashrc so that whenever you open python from the terminal the correct version opens.

  • If both of the above methods don't work for you then check this :

ImportError No module named or this

Gan answered 20/5, 2016 at 8:49 Comment(5)
You were right Ani Menon, there are two versions of python installed. I explained in an answer how I proceeded to solve the problem.Abelmosk
Good you may also fix it using aliases. If both your python versions are assigned different aliases it will be easy to use both as required. (I don't know why someone gave a down-vote to the answer!)Gan
Interesting, could you expand a bit more about the aliases?Abelmosk
Using alias command; answer edited. Similarly, you may alias pip as well.Gan
Do a brew reinstall python2. It might complain it can't delete some files. If that's the case, make sure all files in your /usr/local/lib/python2.7/site-packages are owned by you (i.e. cd /usr/local/lib/python2.7/site-packages ; sudo chown -R <YOURLOGIN> *). Then retry brew reinstall python2 and pip install --upgrade pip setuptools and finally use pip to install your library. pip install X.Prudy
A
26

Here the answer that worked, which is basically what has been explained in the comments of the question. However, I thought it would be useful to have it explained as a clear and well structured answer.

As highlighted, the problem was that I was not using the interpreter that pip was installing for. The command which shows where pip was installing the modules:

$ which -a pip
/usr/local/bin/pip

and where the different python versions were located:

$ which -a python
/usr/bin/python
/usr/local/bin/python

That is, my system/default python was

/usr/bin/python

while pip was installing for

/usr/local/bin/python

Therefore, I could not import anything I installed when I just typed python, because the /usr/bin/python interpreter was the one started.

Solution

Install pip again specifying the destination of the modules that will be installed. This must be the destination for the system/default python.

This has been done in two steps:

  1. Downloding get-pip.py from bootstrap.pypa.io/get-pip.py. (You may need to use the deprecated one for Python 2: bootstrap.pypa.io/2.7/get-pip.py)

  2. Installing it with the following command

    sudo /usr/bin/python get-pip.py

Note that without the sudo I got an error and was not able to install pip.

Abelmosk answered 20/5, 2016 at 10:31 Comment(2)
Hi, I was facing a similar same issue. In my case, in Mac OS High Sierra, which -a pip yields /usr/local/bin/pip and which -a python yields /usr/bin/python. I tried the same procedure, but I am failing. I downloaded get-pip.py into my desktop and ran sudo /usr/bin/python ~/Desktop/get-pip.py. The procedure runs fine, but when I run which -a pip, I still only get /usr/local/bin/pip.Difference
I had the same problem. In my case, it also was located in /usr/local/bin/pip before and after install. So you could say nothing changed, but in fact, something changed, because now it's working. sudo was not required for me. I'm running Mojave.Piscary
N
18

I have just fixed a similar issue.

To give some background, I install pip with homebrew by executing brew install python. One drawback by executing this command, it will install both python2 and python3(maybe not a disadvantage in some case), then

pip install scrapy

but when I try to import scrapy, it complained ImportError: No module named scrapy.


My Solution: run brew doctor, it should report you a link is broken, it asks you to run brew link python, you might encounter some errors, but follow the prompt suggestion to move forward, after successfully executing brew link python, everything should work now.

Nathalie answered 28/2, 2017 at 3:41 Comment(0)
G
10

Since your problem maybe caused due to various reason, I have listed down a few of them here :

The link you were looking for : https://pythonhosted.org/setuptools/setuptools.html#development-mode

  • It may also happen if you have two versions of python installed. If the pip that you are accessing is of one version & the python interpreter used is another.

So just see to that you are using the same version of python to install and use the package.

You may fix this using alias,

First, set up a shell alias:

alias python=/usr/local/bin/python3

Then, type that at a prompt, or put it in your ~/.bashrc so that whenever you open python from the terminal the correct version opens.

  • If both of the above methods don't work for you then check this :

ImportError No module named or this

Gan answered 20/5, 2016 at 8:49 Comment(5)
You were right Ani Menon, there are two versions of python installed. I explained in an answer how I proceeded to solve the problem.Abelmosk
Good you may also fix it using aliases. If both your python versions are assigned different aliases it will be easy to use both as required. (I don't know why someone gave a down-vote to the answer!)Gan
Interesting, could you expand a bit more about the aliases?Abelmosk
Using alias command; answer edited. Similarly, you may alias pip as well.Gan
Do a brew reinstall python2. It might complain it can't delete some files. If that's the case, make sure all files in your /usr/local/lib/python2.7/site-packages are owned by you (i.e. cd /usr/local/lib/python2.7/site-packages ; sudo chown -R <YOURLOGIN> *). Then retry brew reinstall python2 and pip install --upgrade pip setuptools and finally use pip to install your library. pip install X.Prudy
S
4

I was able to fix this problem by running:

$ brew doctor 

and got the following:

Consider setting your PATH so that /usr/local/bin occurs before /usr/bin.

Here is a one-liner:

echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.bash_profile

Once I ran the one-liner, I was able to access the installed package from /usr/local/bin

Sketchy answered 6/11, 2019 at 20:44 Comment(0)
P
2

I'm adding this in case it helps anyone else out. For me the issue was I was running Anaconda and pip3 was installing to a different directory than Anaconda was linked with. To fix this run conda deactivate. You can reactivate later with conda activate

Personal answered 14/4, 2019 at 19:18 Comment(1)
For me it was just pip in general, it would install but wasn't available to import. Needed to use pip3 install <module name> instead of pip install <module name> and then it becomes available to Python 3. This on Mac where either Python 2 is available only or both Python 2 and Python 3 are installed.Goldschmidt
N
0

Run brew doctorin the terminal it should give you a warning that says:

Warning: The following directories do not exist:
/usr/local/sbin

You should create these directories and change their ownership to your account.
  sudo mkdir -p /usr/local/sbin
  sudo chown -R $(whoami) /usr/local/sbin

type sudo mkdir -p /usr/local/sbin in the terminal and hit enter then type sudo chown -R $(whoami) /usr/local/sbin then run python and try importing your module again.

Nonflammable answered 17/9, 2018 at 23:7 Comment(0)
L
0

In my case I changed the VSCode interpreter to ~/opt/anaconda3/bin/python. This fixed the issue.

Ludovick answered 8/1, 2023 at 6:50 Comment(0)
I
0

For me the problem was that pip3.11installs new modules to /usr/local/lib/python3.11(or whatever your python version is) which is not a path the python binary considers when looking for modules.

As a workaround you can specify the modules path in the variable PYTHONPATH like

PYTHONPATH="/usr/local/lib/python3.11/site-packages/" python3.11 my_script.py

Alternatively one can specify a directory where pip should new modules to with the -toption.

# print module paths which are considered by python
python3.11
Python 3.11.3 (main, Apr  8 2023, 04:18:45) [Clang 14.0.0 (clang-1400.0.29.102)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/opt/local/Library/Frameworks/Python.framework/Versions/3.11/lib/python311.zip', '/opt/local/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11', '/opt/local/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/lib-dynload', '/opt/local/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages']
# install new module to one of the above listed directories
pip3.11 install my_module -t /opt/local/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages
Izabel answered 16/8, 2023 at 20:50 Comment(0)
N
0

For some specific cases such as crypto module in python, after I install it in Pycharm, I will have to locate to the "crypto" module path and then change the directory name to "Crypto".

Necessitous answered 25/9, 2023 at 4:2 Comment(1)
The usage of crypto/pycrypto is highly discouraged for security reasons.Thematic

© 2022 - 2024 — McMap. All rights reserved.