ModuleNotFoundError
Linux Linux
Severity: MinorWhat Does This Error Mean?
A Python ModuleNotFoundError on Linux means a Python script is trying to import a library or module that is not installed in the Python environment being used. The full error looks like: 'ModuleNotFoundError: No module named xyz.' This is extremely common and is almost always fixed by installing the missing module with pip.
Affected Models
- Ubuntu
- Debian
- Fedora
- CentOS
- Arch Linux
- Linux Mint
- openSUSE
Common Causes
- The required Python module has not been installed with pip or the system package manager
- The module is installed for a different Python version (e.g., Python 2 instead of Python 3)
- A virtual environment is active but the module is installed outside of it
- The module was installed system-wide but the script is running in a different environment
- The module name was misspelled in the import statement
How to Fix It
-
Install the missing module with pip. Type: pip install [module-name] — or if pip3 is your default: pip3 install [module-name] — replace [module-name] with the module named in the error.
The module name in the error message is usually the same as the pip package name, but not always. For example, the 'cv2' module is installed with: pip install opencv-python
-
Check which Python version you are running. Type: python --version and python3 --version — make sure you install the module for the same version running your script.
Many Linux systems have both Python 2 and Python 3. Installing with 'pip' may install for Python 2 while your script runs with Python 3. Use 'pip3' for Python 3.
-
Check if a virtual environment is active. Type: echo $VIRTUAL_ENV — if this shows a path, you are inside a virtual environment. Install the module inside it: pip install [module-name]
Virtual environments are isolated Python installations. Modules installed outside the environment are not visible inside it.
-
Check if the module is available as a system package. Some modules are available through apt: sudo apt search python3-[module-name] — installing the system package avoids pip conflicts.
On Debian/Ubuntu, many Python modules have corresponding apt packages like 'python3-numpy' or 'python3-requests' that integrate cleanly with the system Python.
-
Verify the module is installed correctly. After installing, type: python3 -c 'import [module-name]; print([module-name].__version__)' — if this prints a version number, the module is installed and working.
This quick test confirms the installation worked before trying to run the full script again.
When to Call a Professional
This is a standard developer/user issue on Linux and never requires professional service help. If you are running a script someone else provided, ask the script author for the full list of dependencies.
Frequently Asked Questions
Why does the module work in one terminal but not another?
This almost always means different Python environments are active in each terminal. One terminal may have a virtual environment activated (check for (venv) at the start of the prompt) while the other uses the system Python. Activate the same virtual environment in both terminals, or install the module in all environments where the script will run.
I installed the module but still get the error. What is happening?
The most common reason is that the module was installed for the wrong Python version. Run: python3 -m pip install [module-name] — using python3 -m pip ensures you install for the exact Python 3 interpreter you are using. Also check if you accidentally have two Python 3 installations (common on systems that installed Anaconda).
What is a virtual environment and should I use one?
A virtual environment is an isolated Python installation that keeps a project's dependencies separate from the system Python. This prevents version conflicts between different projects. For any serious development work, virtual environments are strongly recommended. Create one with: python3 -m venv myenv — activate it with: source myenv/bin/activate — then install packages inside it normally with pip.