user3654181
user3654181

Reputation: 461

"ImportError: no module named 'requests'" after installing with pip

I am getting ImportError : no module named 'requests'.

But I have installed the requests package using the command pip install requests.

On running the command pip freeze in the command prompt, the result is

requests==2.7.0

So why is this sort of error happening while running the python file?

Upvotes: 22

Views: 131162

Answers (8)

glades
glades

Reputation: 4884

Had the same problem using conda. pip show | grep <module> showed the module, but once I went on to run the script using python myscript.py it didn't recognise it.

The problem was that even when I executed pip from within the conda env, it was not the virtual env pip that was executed but another one. To check which pip is used try:

which -a pip

In my case my conda env was "user_queries", but the pip version used was /home/linuxbrew/.linuxbrew/bin/pip.

/home/linuxbrew/.linuxbrew/bin/pip
/home/linuxbrew/.linuxbrew/bin/pip
/home/myuser/anaconda3/envs/user_queries/bin/pip
/usr/bin/pip
/bin/pip

An easy fix is to just install your requirements using the absolute path to the virtualenv pip installation like so:

/home/myuser/anaconda3/envs/user_queries/bin/pip install -r requirements.txt

or

/home/myuser/anaconda3/envs/user_queries/bin/pip install requests

Upvotes: 0

kwick
kwick

Reputation: 797

Run

pip show <module name>

and check the module causing issue, is in which location, this will tell you which Python installation you should use as your interpreter or else you can copy the module files manually to your current Python interpreter installation's location.

enter image description here

Upvotes: 0

Tanzir Rahman
Tanzir Rahman

Reputation: 185

Opening CMD in the location of the already installed request folder and running "pip install requests" worked for me. I am using two different versions of Python.

I think this works because requests is now installed outside my virtual environment. Haven't checked but just thought I'd write this in, in case anyone else is going crazy searching on Google.

Upvotes: 0

Pascal Louis-Marie
Pascal Louis-Marie

Reputation: 252

if it works when you do :

python
>>> import requests

then it might be a mismatch between a previous version of python on your computer and the one you are trying to use

in that case : check the location of your working python:

which python And get sure it is matching the first line in your python code

#!<path_from_which_python_command>

Upvotes: 0

dp2050
dp2050

Reputation: 342

In Windows it worked for me only after trying the following: 1. Open cmd inside the folder where "requests" is unpacked. (CTRL+SHIFT+right mouse click, choose the appropriate popup menu item) 2. (Here is the path to your pip3.exe)\pip3.exe install requests Done

Upvotes: 1

Albert
Albert

Reputation: 10717

I had this error before when I was executing a python3 script, after this:

sudo pip3 install requests

the problem solved, If you are using python3, give a shot.

Upvotes: 5

Isak La Fleur
Isak La Fleur

Reputation: 4668

Run in command prompt.

pip list

Check what version you have installed on your system if you have an old version.

Try to uninstall the package...

pip uninstall requests

Try after to install it:

pip install requests

You can also test if pip does not do the job.

easy_install requests

Upvotes: 14

Bruce Chou
Bruce Chou

Reputation: 356

One possible reason is that you have multiple python executables in your environment, for example 2.6.x, 2.7.x or virtaulenv. You might install the package into one of them and run your script with another.

Type python in the prompt, and press the tab key to see what versions of Python in your environment.

Upvotes: 2

Related Questions