I've installed the package using pip, but I got "ImportError: No Module Named [x]"
Check the following points to solve the problem.
1. Does pip fail to install the package?##
You can check which packages are installed with:
Hightlight specific package:
# Linux or other Unix-like systems pip list | grep -i <your-package-name> # Windows pip list | findstr /I <your-package-name>
If nothing is displayed, It is likely that something went wrong during the installation. You should reinstall and pay attention to the error message during the process.
2. Are pip and python consistent?##
Show pip info:
pip -V # output: # pip 20.0.2 from /usr/local/lib/python3.5/dist-packages/pip (python 3.5)
Show python info:
# show python version python -V # output: # Python 2.7.13 # show python module path python -c "import sys; print('\n'.join(sys.path))" # output: # /usr/lib/python2.7 # /usr/lib/python2.7/plat-x86_64-linux-gnu # /usr/lib/python2.7/lib-tk # /usr/lib/python2.7/lib-old # /usr/lib/python2.7/lib-dynload # /home/nomodulenamed/.local/lib/python2.7/site-packages # /usr/local/lib/python2.7/dist-packages # /usr/lib/python2.7/dist-packages # /usr/lib/python2.7/dist-packages/gtk-2.0
If the python version number in the pip information is inconsistent with the python information, or the file path of pip and the library path of python do not match, you should reinstall your package with correct pip.
In the example, python version number in the pip information is
(python 3.5), python version number in the python information is
(Python 2.7.13). They are inconsistent.
How to use the correct pip?
The easiest way is using
python -m pip
python -m pip -V # output: # pip 9.0.1 from /usr/lib/python2.7/dist-packages (python 2.7) # install your package python -m pip <your-package-name>
3. You must be careful when using sudo! (Unix-like systems only)##
In some cases,
sudo pippoint to different environments
pip -V # output: # pip 20.0.2 from /usr/local/lib/python3.5/dist-packages/pip (python 3.5) sudo pip -V # output: # pip 9.0.1 from /usr/lib/python2.7/dist-packages (python 2.7)
Make sure you are using the correct pip, and so is python.
How to use current pip under sudo?
Use the full path of the current pip:
which pip # output: # /home/nomodulenamed/venv/bin/pip sudo /home/nomodulenamed/venv/bin/pip -V # output: # pip 20.0.2 from /usr/local/lib/python3.5/dist-packages/pip (python 3.5)
4. Does the program use another python environment?##
Many python programs use virtual environments (virtualenv, pipenv, ...), The virtual environment must be activated before installing the package.
In order to check the python environment in use, add the following code in the program entry file:
import sys print(sys.executable)
If it does not meet expectations, then run:
<printed-sys.executable> -m pip install <your-package-name> # assume printed sys.executable is `/home/nomodulenamed/venv/bin/python`, then run /home/nomodulenamed/venv/bin/python -m pip install <your-package-name>
5. Did you install multiple versions of package?##
If you installed the package through a non-pip command, for example unzip pakcage and
python setup.py install, may cause multiple versions of packages are installed.
Uninstall multiple times before reinstalling:
pip uninstall -y <your-package-name> pip uninstall -y <your-package-name> pip uninstall -y <your-package-name> # Until the system prompts that the package is not installed pip install <your-package-name>
6. Does dependency version conflict?##
In an application, I installed:
- a. package A, which requires package
C == 2.1
- b. package B, which requires package
C == 3.0
When the module in package A is running, it is likely raise a module not found error.
There is no universal solution, but there are suggestions to help you avoid this problem
- a. As far as possible using only well-maintained, famous package.
- b. Do not frequently update the version of dependent packages.
- c. Use
pipenvto lock the version number of dependent packages.
- a. package A, which requires package
7. Python2 or Python3 only##
There are some ancient packages that only support the python2 environment, while more and more packages only support the python3 environment.
The only way is to maintain it yourself.
Python3 is the future, use it whenever possible.
Look forward to your feedback! Click the button in the feedback area in the upper right corner to choose whether it helps.
Created: 2020-04-30 13:51