18

I'm trying to test if requests module has been well installed. But I'm 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 have installed requests with pip, and pip list command gives the following result :

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 requests isn't 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)
5
  • 3
    Are you sure the pip environment is the same as the python one? Do which pip and which python. It could be you are installing for Python3 and running with Python27 Commented May 20, 2017 at 20:11
  • @OptimusCrime see my edit
    – mounaim
    Commented May 20, 2017 at 20:13
  • 1
    Can you also run python --version and pip --version? Commented May 20, 2017 at 20:14
  • @OptimusCrime see my edit please
    – mounaim
    Commented May 20, 2017 at 20:15
  • 1
    pip is not pointing to the same environment as your python. You need to look at your PATH variable... see if you have pip installed for your python, e.g. /Library/Frameworks/Python.framework/Versions/2.7/bin/pip or (pip2, pip2.7).
    – AChampion
    Commented May 20, 2017 at 20:17

5 Answers 5

13

In general, you should get into the habit of working in a virtualenv. I find the documentation here to be helpful.

If you install all of your dependencies within the virtual environment, you'll be (mostly) sure that you are installing those deps. in the same environment that you're running the jobs in.

For your case, on the command line go to the directory where your code lives and run

pip install virtualenv
virtualenv my_project
source my_project/bin/activate

Now that the virtualenv is active you can

pip install requests

Only what is installed in the virtualenv will be available. This will keep your system clean. Each project should get its own virtualenv, meaning only the dependencies needed for each project will be available to them. This way you could, say, have version 1 of some dependency installed for one project and version 2 for another. They won't come into conflict.

After you have installed all the dependencies, run

pip freeze > requirements.txt

To get a list of all the dependencies for the project saved. Next time you need to install these, you simply run

pip install -r requirements.txt

Once you are done working in the virtualenv, run

deactivate

UPDATE:

It's now 2024 and there are more options for managing environments and dependencies. I won't try to provide an exhaustive list here, but they all provide the same core capabilities and have similar but different commands to do these things. Among the options are:

  • venv
  • virtualenvwrapper
  • pyvenv
  • mamba
  • conda (mostly for data science and machine learning projects)
  • Poetry (my current favorite)
  • uv (built in rust, very fast, as of now still a bit new)
1
  • I did the whole pip freeze > requirements.txt from within a virtualenv, and then, in another folder, copied over requirements.txt, created a new virtualenv, ran source env/bin/activate then pip install -r requiremnts.txt and got the error no module named requests. But, running pip install requests again (even though it was in the requirements.txt file all along) fixed the issue. Weird but that's what happened to me. Commented Sep 11, 2020 at 22:15
3

I am not 100% sure, but the paths from which python and which pip may indicate that you have two versions installed. The Python version being the old one that was shipped with OS X, and another version.

I would advice you to install Python27 (or even better Python3) from brew.

You can install brew with a single command, and another one for installing Python27/3. When this is done you set the PATH variable in your shell rc file and you should be good to go.

I have Python27 installed (via brew) and my (working 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)
2
  • How can I edit the Path so python point to the version shipped with OSX ?
    – mounaim
    Commented May 20, 2017 at 20:22
  • 1
    @mounaim It is exampled in the link I provided. You edit the file ~/.profile and insert export PATH=/usr/local/bin:/usr/local/sbin:$PATH Commented May 20, 2017 at 20:24
1

I experienced this same issue on Ubuntu 18.04 LTS, to check that, I first checked if requests library was installed on my system or not.

Run these commands on your terminal inside the virtual environment in which you working

$ python

Then the python command line opens, then run

>>> import requests

After this if you get an ImportError saying, No module named requests, then it means the dependency has not been installed properly. If there is no such error, then it means the dependency is installed successfully.

0

This can occur for example if pip is actually pip3 and python is actually python2.7. In your case the which pip and which python eliminate this possibility but it just happened to me.

The solution was to do pip2 instead of pip; if the situation was reversed you can use pip3.

0

Simply go inside your virtual environment and run below commands:

1). pip install --user pipenv 2). pipenv install requests

after executing the above commands cd to your app folder inside the virtual environment and just run it. Hopefully now it will run.

Reference link: https://python-guide-pt-br.readthedocs.io/pt_BR/latest/dev/virtualenvs.html#make-sure-you-ve-got-python-pip

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.