Search on blog:

Python: How can't Python import module after installation with pip

Sometime after installation

pip install some_module

Python can't find this module and shows error ModuleNotFoundError

import some_module

ModuleNotFoundError: No module named 'some_module'

Not so often problem is that module uses different name for installation and different for import. It can be totally different name - like install python-some_module and import some_module - or only upper/lower chars - like install some_module and import SomeModule.

But often problem is because you may have installed many versions of Python (which is popular on Linux) and pip installs module in different version then you run with command python

Every version of Python has own folder for modules because it may need module compiled specially for this version.

First you can check versions (upper V)

pip -V

python -V

You may also check if you don't have also similar commands

pip3 -V

pip3.7 -V
pip3.8 -V
pip3.9 -V

python3 -V

python3.7 -V
python3.8 -V
python3.9 -V

All these names with numbers let you simply run code with specific version.

You can use pip3.9 to make sure you install it for commands python3.9

But sometimes the simplest method is to use directly python to run module pip and install module with correct python.

python -m pip install some_module

it is always pip, not pip3, pip3.7 etc

python3 -m pip install some_module

python3.7 -m pip install some_module
python3.8 -m pip install some_module
python3.9 -m pip install some_module

Later you can try to clean this mess and put correct pip3.x in place of pip.

On Linux (and probably on MacOS too) you can find folder with pip using

which pip

which pip3

eventually

whereis pip

whereis pip3

All versions should be in the same folder (ie. /usr/local/bin) You can go to folder with pip and pip3.x, remove pip and create symbolic link to other version using

ln -s pip3.9 pip

# or

ln -s pip3.9 pip3

instead of copying file.

The same you can do with python3.9 and python3 If you still use Python 2 (or system uses it) and command python runs Python 2 then better don't put python3 in places of python

If you like it
Buy a Coffee