2

I am writing a code in Python 2.7.9 for which I need the requests module. I installed the module using sudo pip install requests but still in python 2.7.9 I am getting an error as follows:

Python 2.7.9 (default, Jan 5 2016, 18:47:14) [GCC 4.8.4] on linux2 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 

I checked the installation location /usr/local/lib/python2.7/dist-packages, it contains the requests package directory. The same thing works fine in python3, so I am guessing it's an installation error. How can I fix it ?

EDIT:

On executing pip lists, I could see requests (2.1.9) in the list. After I ran pip uninstall requests it shows requests (2.2.1) in pip list.

10
  • 1
    try printing sys.path to see where its looking ... Commented Jan 6, 2016 at 4:05
  • ['', '/usr/local/lib/python27.zip', '/usr/local/lib/python2.7', '/usr/local/lib/python2.7/plat-linux2', '/usr/local/lib/python2.7/lib-tk', '/usr/local/lib/python2.7/lib-old', '/usr/local/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/site-packages'] Commented Jan 6, 2016 at 4:08
  • edit your original question ... .... but I dont see dist-packages in that list ... so how did you install requests? Commented Jan 6, 2016 at 4:09
  • sudo pip install requests Commented Jan 6, 2016 at 4:10
  • type which pip ? after you uninstalled it could you import it(since it still was in the pip list command)? my guess is you have an apt-get install python-requests conflicting with your pip install requests ... what if you try to download the zip, unzip it, and run python2.7 setup.py install ? Commented Jan 6, 2016 at 4:10

1 Answer 1

3

Based on the comments, it seems you have installed python 2.7.9 using a method Ubuntu doesn't like. Because of this the dist-packages folder is not added to your sys.path. You could set the PYTHONPATH variable in your .bashrc (or other zshrc, etc) to add that folder to your sys.path by default.

Better method would be to use a library like pyenv (It handles all dependency issues flawlessly for multiple python versions) or a better supported ppa for the latest python where this problem shouldn't arise at all.

Also, you have 2 versions of requests. This seems to be because one is installed using apt-get (sudo apt-get install python-requests) and the other is from pip (sudo pip install requests). It would be good to remove one of them to avoid confusion.

Sign up to request clarification or add additional context in comments.

2 Comments

I installed Python 2.7.9 building from the source.
Yep, the source python installation doesn't add dist-packages by default - because it is not a default location (only in Ubuntu). Try a ppa or pyenv - would be the best method. (I prefer pyenv as I can change the python version on the fly)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.