22

writing some python in OS X, and it's saying several packages I installed with pip "ImportError: no module named requests"

When running pip install requests

> sudo -H pip install requests 
Requirement already satisfied: requests in /usr/local/lib/python2.7/site-packages 
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python2.7/site-packages (from requests) 
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /usr/local/lib/python2.7/site-packages (from requests) 
Requirement already satisfied: urllib3<1.22,>=1.21.1 in /usr/local/lib/python2.7/site-packages (from requests) 
Requirement already satisfied: idna<2.6,>=2.5 in /usr/local/lib/python2.7/site-packages (from requests)

it's erroring on twindb_cloudflare, requests, group, tarutil, and MySQL DB. I'm able to run the exact same imports on another script (in the same folder) without error.

Cédric Julien
  • 74,806
  • 15
  • 120
  • 127
zoey cluff
  • 383
  • 2
  • 4
  • 10

10 Answers10

19

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
Abhishek Parikh
  • 911
  • 20
  • 39
11

In my case, I was running a python version included with Jupyter, which installs modules in a different place than the default one apparently. I found that out by running this in Jupyter:

import sys
sys.executable

which gave me

'/usr/local/Cellar/jupyterlab/3.0.13/libexec/bin/python3.9'

I was then able to run the following command in Jupyter:

!/usr/local/Cellar/jupyterlab/3.0.13/libexec/bin/python3.9 -m pip install scipy matplotlib

and then I was able to import my modules:

import scipy.stats
import matplotlib
Aymeric Bouzy aybbyk
  • 1,646
  • 2
  • 19
  • 28
8

i had the same issue(also in MAC OS) and solved it by instead running the python script with python3: python3 script.py

in my case i was also using pip3 and not pip.

Daniel Badyan
  • 109
  • 2
  • 5
6

I suffered from this problem and finally found the solution.

Defaulting to user installation because normal site-packages is not writeable

Requirement already satisfied: django in /usr/lib/python3/dist-packages (2.2.12)

use 'sudo' to crossoff user installation issue;

and pip install --target=/usr/local/python3.7/site-packages --upgrade {module_name}

someone mentioned this and worked for me.

cUteDeVil
  • 61
  • 1
  • 2
5

If you are using a Mac, it could be that you installed the modules with pip (meaning python2, but you execute your code with python3 which does not have the modules you installed for python2).

Mac has python version 2 set as default and usually does not come with pip preinstalled or is linked with version 2. I recommend leaving it that way. Use version 3 for your personal use cases and leave your Mac with version 2 as default. As you have to install python3 yourself, means you might also want to check/install pip3.

Check if you have python 3 installed:

python3 --version

Check if you have pip3 installed (usually included by default since python 3.4):

pip3 --version

Check what packages you have installed with pip3:

pip3 list

If you use an editor tool, make sure to set it to use python3 when running your file.

Example for VS Code: Set VS Code to use Python3 on the bottom right corner, which you should see when having a .py file open: Set Python version

And now if you want to import any modules into python, make sure to install them with pip3:

pip3 install package_name

If you run into permission issue, you might consider to run the command with sudo rights:

sudo pip3 install package_name
Flavio Caduff
  • 580
  • 7
  • 12
1

Just to share my findings and perhaps a newbie error, but in my case mariadb was installed using 'sudo pip3 install mariadb' and I tried to run my script with 'python myscript.py' and that didn't work. But I worked when I use 'python3 myscript.py', seems to be that it matters which pyhton you use.

Rob
  • 11
  • 1
0

My Raspberry Pi 4 did not recognise the ipython command and the following was displayed when attempting to install iPython:

me@raspberrypi:~ $ sudo pip3 install ipython
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: ipython in /usr/lib/python3/dist-packages (5.8.0)
Requirement already satisfied: pexpect in /usr/lib/python3/dist-packages (from ipython) (4.6.0)

I knew this wasn't a PATH issue as iPython is a pip package.

I found an IPython directory under /usr/lib/python3/dist-packages/ and inside this directory I found program __main__.py.

Running __main__.py started iPython:

python3 /usr/lib/python3/dist-packages/IPython/__main__.py

I added the following alias to my .bashrc file to allow me to simply type ipython.

alias ipython='python3 /usr/lib/python3/dist-packages/IPython/__main__.py'

Hope the above is helpful. God bless

Steve
  • 666
  • 7
  • 14
0

i have also same issue with PyPDF2. one day before it was working but now i am unable to import. while i am installing again it show me that already installed.

here we need to know that where it installed and from where it imports. when both path will match then may be this issue will get resolve.

OR if there is version issue we need to understand version compatibility. which version of PyPDF2 will support with python 3.8xxx

0

If you have Python(python2) installed you then you can use the following command to install pip(for python2).

curl https://bootstrap.pypa.io/pip/2.7/get-pip.py -o get-pip.py
python get-pip.py

Now you can check for pip2 and can try to run your program

pip2 --version

I hope these will help you

0

Method 1

If you system having more than one python version installed, like in my ubuntu by default python versions are python and python3, then pip also have different versions like pip and pip3. So in this situation access pip by specific python version with -m like

python3 -m pip install package_name

or

python -m pip install package_name

Make sure the use of sudo if you are on linux.

Method 2

In 2nd method you need to find installation directory (from where python find modules). For this run the following python script

import sys
print(sys.path)

In my case it returns

['', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload', '/home/mudasir_habib/.local/lib/python3.8/site-packages', '/usr/local/lib/python3.8/dist-packages', '/usr/lib/python3/dist-packages']

These are the paths that python use to find modules, I copy the dist-packages path. So now run the command by providing target path for installation like below

python -m pip install package_name -t /your/copied/path

In my case i run the following command that solve my problem

sudo python3 -m pip install keras -t /usr/lib/python3/dist-packages

I hope it may helpful

Mudasir Habib
  • 385
  • 2
  • 9
  • Can't we set the target path permanently or do we have to define everytime we install a package? – Mubarak Abdullah Dec 20 '21 at 13:37
  • Normally pip manages all the things by itself. But some times when we have installed multiple python and pip version installed, then pip confused to find correct path so we need to provide manually. – Mudasir Habib Dec 21 '21 at 07:52