bawejakunal
bawejakunal

Reputation: 1708

Python 2.7.9: No module named requests

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.

Upvotes: 2

Views: 2922

Answers (1)

AbdealiLoKo
AbdealiLoKo

Reputation: 3317

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.

Upvotes: 3

Related Questions