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

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.


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.