Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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:

  • installing pyside using PIP - nmake not found
  • PIp installs but module is not found ==> might have provided the right answer, but the links given do not work anymore
  • google.protobuf installed, but module not found
  • Python pip install module is not found. How to link python to pip location?

Any ideas of what the problem is?

like image 649
J0ANMM Avatar asked May 20 '16 08:05

J0ANMM


People also ask

How do I fix pip module not found?

The Python "ModuleNotFoundError: No module named 'pip'" occurs when pip is not installed in our Python environment. To solve the error, install the module by running the python -m ensurepip --upgrade command on Linux or MacOS or py -m ensurepip --upgrade on Windows.

Why can't Python find my module?

This is caused by the fact that the version of Python you're running your script with is not configured to search for modules where you've installed them. This happens when you use the wrong installation of pip to install packages.

Where do pip packages get installed on Mac?

pip when used with virtualenv will generally install packages in the path <virtualenv_name>/lib/<python_ver>/site-packages .

What is the difference between pip install and import?

The Python pip utility is used to install a module, but the import command is used to actually import the module. Python includes some built-in standard modules. These modules are part of the Python Standard Library, also known as the Library Reference.


2 Answers

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.

like image 186
J0ANMM Avatar answered Sep 22 '22 04:09

J0ANMM


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.

like image 38
Raymond Avatar answered Sep 20 '22 04:09

Raymond