"No modules with requests" even if I installed requests with pip
I am trying to check if the requests module is installed correctly. But I am getting the following error:
raceback (most recent call last):
File "/Users/macbookpro/Desktop/test.py", line 1, in <module>
import requests
ImportError: No module named requests
when trying to run the following test script:
import requests
print 'test'
But I installed requests with pip and the command pip list
gives the following output:
MBPdeMacBook2:~ macbookpro$ pip list
arrow (0.7.0)
beautifulsoup4 (4.4.1)
classifier (1.6.5)
coursera-dl (0.6.1)
Django (1.8.6)
html5lib (1.0b8)
keyring (9.0)
lxml (3.6.0)
Pillow (3.4.2)
pip (8.0.2)
pyasn1 (0.1.9)
requests (2.14.2)
setuptools (19.4)
six (1.10.0)
urllib3 (1.16)
vboxapi (1.0)
virtualenv (13.1.2)
wheel (0.26.0)
Why are queries not being imported?
EDIT:
MBPdeMacBook2:~ macbookpro$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
MBPdeMacBook2:~ macbookpro$ which pip
/usr/local/bin/pip
MBPdeMacBook2:~ macbookpro$ python --version
Python 2.7.11
MBPdeMacBook2:~ macbookpro$ pip --version
pip 8.0.2 from /usr/local/lib/python2.7/site-packages (python 2.7)
source to share
I'm not 100% sure, but paths from which python
and which pip
may indicate that you have two versions installed. The Python version is the old one that shipped with OS X and a different version.
I would advise you to install Python27 (or even better Python3) from brew.
You can install brew with one command and another one to install Python27 / 3. When that is done, you will set a variable PATH
in your rc shell file and you should be good to go.
I have Python27 installed (via brew) and my (work environment) reports the following paths:
which python: /usr/local/bin/python
which pip: /usr/local/bin/pip
and
python --version: 2.7.15
pip --version: pip 9.0.1 from /usr/local/lib/python2.7/site-packages (python2.7)
source to share
In general, you should get used to working in virtualenv
. I found the documentation here to be helpful.
If you install all your dependencies in a virtual environment, you will be (mostly) sure that you are installing those deps. in the same environment in which the jobs run.
In your case change to the directory where the code is on the command line and run
pip install virtualenv
virtualenv my_project
source my_project/bin/activate
Now that virtualenv is active you can
pip install requests
Only what is installed in virtualenv is available. This will keep your system clean. Each project should get its own virtualenv, which means that only the dependencies needed for each project will be available to them. So you can, say, have version 1 of some dependency installed for one project and version 2 for another. They will not come into conflict.
After installing all dependencies, run
pip freeze > requirements.txt
Get a list of all dependencies for a saved project. The next time you need to install them, you just run
pip install -r requirements.txt
Once you are done in virtualenv, run
deactivate
source to share