ModuleNotFoundError: No module named ‘XXX’ in Python
The Python “ModuleNotFoundError: No module named ‘XXX’” occurs when we forget to install the XXX module before importing it or install it in an incorrect environment. To solve the error, install the module by running the pip install XXX command.
Open your terminal in your project’s root directory and install the XXX module.
notice:
👇️ in a virtual environment or using Python 2
pip install XXX
👇️ for python 3 (could also be pip3.10 depending on your version)
pip3 install XXX
👇️ if you get permissions error
sudo pip3 install XXX
👇️ if you don’t have pip in your PATH environment variable
python -m pip install XXX
👇️ for python 3 (could also be pip3.10 depending on your version)
python3 -m pip install XXX
👇️ for Anaconda
conda install -c anaconda XXX
The Python error “ModuleNotFoundError: No module named ‘XXX’” occurs for multiple reasons:
- Not having the XXX package installed by running pip install XXX.
- Installing the package in a different Python version than the one you’re using.
- Installing the package globally and not in your virtual environment.
- Your IDE running an incorrect version of Python.
- Naming your module XXX.py which would shadow the official module.
- Declaring a variable named XXX which would shadow the imported variable.
If the error persists, get your Python version and make sure you are installing the package using the correct Python version.
For example, my Python version is 3.10.4, so I would install the XXX package with pip3.10 install XXX.
pip3.10 install XXX
👇️ if you get permissions error use pip3 (NOT pip3.X)
sudo pip3 install XXX
If the “No module named ‘XXX’” error persists, try restarting your IDE and development server / script.
You can check if you have the XXX package installed by running the pip show XXX command.
👇️ check if you have XXX installed
pip3 show XXX
👇️ if you don’t have pip setup in PATH
python3 -m pip show XXX
The pip show XXX command will either state that the package is not installed or show a bunch of information about the package, including the location where the package is installed.
If the package is not installed, make sure your IDE is using the correct version of Python.
If you have multiple Python versions installed on your machine, you might have installed the XXX package using the incorrect version or your IDE might be setup to use a different version.
For example, In VSCode, you can press CTRL + Shift + P or (⌘ + Shift + P on Mac) to open the command palette.
Then type “Python select interpreter” in the field.
Your IDE should be using the same version of Python (including the virtual environment) that you are using to install packages from your terminal.
👇️ check if you have XXX installed
pip3 show XXX
👇️ if you don’t have pip setup in PATH
python3 -m pip show XXX
👇️ uninstall XXX
pip3 uninstall XXX
👇️ if you don’t have pip setup in PATH
python3 -m pip uninstall XXX
👇️ install XXX
pip3 install XXX
👇️ if you don’t have pip setup in PATH
python3 -m pip install XXX
related blog link: