ImportError: No module named X (Fix)

This error happens when Python tries to import a module but cannot find it.

In many cases, the fix is simple:

  • The package is not installed
  • It was installed in the wrong Python environment
  • The import name is wrong
  • Your own file name is blocking the real module

Start with the fastest checks first. That usually solves the problem quickly.

Quick fix

python -m pip install package_name

Use the same Python interpreter that runs your script. Replace package_name with the real package name.

For example:

python -m pip install requests

If your system uses python3, try:

python3 -m pip install requests

What this error means

ImportError: No module named X means Python looked for a module and could not find it.

The missing module might be:

  • A third-party package such as requests
  • A local Python file you created
  • A module that exists in a different virtual environment

This page focuses on the specific error message "No module named X".

Common message example

A common example looks like this:

import requests

Possible error:

ImportError: No module named requests

The important part is the name after named.

In this case, Python could not find requests.

Check that exact name before changing anything else.

Most common causes

The most common reasons are:

  • The package is not installed
  • The package was installed in a different Python version or virtual environment
  • The module name is spelled wrong
  • The installed package name and import name are different
  • Your file or folder name shadows the real module
  • You are running the script from the wrong folder
  • The module exists, but Python cannot see its location

Fix 1: Install the missing package

If the missing module is a third-party package, install it first.

Use:

python -m pip install package_name

This is better than plain pip install because it helps pip use the same Python interpreter as your script.

Example:

python -m pip install requests

Then run your script again.

You can also check whether the package is already installed:

python -m pip show requests

If you are new to package installation, see how to install a Python package with pip.

Fix 2: Check the import name

Sometimes the package name and the import name are not the same.

Example:

  • Install with beautifulsoup4
  • Import with bs4

This is correct:

from bs4 import BeautifulSoup

Install command:

python -m pip install beautifulsoup4

Another example:

import PIL

The package is installed with:

python -m pip install Pillow

So if installation worked but the import still fails, check the package documentation and confirm the correct import name.

Fix 3: Check your virtual environment

If you use a virtual environment, you must activate it before installing packages or running your script.

A package installed in one environment is not available in another.

Typical problem:

  • You installed requests globally
  • Your script runs inside a virtual environment
  • That virtual environment does not have requests

Example workflow:

python -m venv .venv

Activate it, then install:

python -m pip install requests

If virtual environments are confusing, read how to create and use a virtual environment in Python.

Fix 4: Look for naming conflicts

Your own file or folder can block Python from importing the real module.

For example, this file name is a problem:

requests.py

Then this code:

import requests

may import your file instead of the real package, or fail with an import error.

Other common bad file names:

  • json.py
  • sys.py
  • random.py

Also check for:

  • A folder with the same name as the package
  • Old __pycache__ files after renaming a file

If needed, rename your file and delete __pycache__.

Example problem:

project/
├─ requests.py
└─ app.py

Inside app.py:

import requests

Fix:

  • Rename requests.py to something like my_requests_test.py
  • Delete any __pycache__ folder
  • Run the script again

Fix 5: Check local files and folders

If you are importing your own module, make sure the file actually exists and is in a place Python can find.

Example folder:

project/
├─ main.py
└─ helpers.py

This works in main.py:

import helpers

print("Imported successfully")

But if helpers.py is missing, moved, or in another folder, Python may raise this error.

Also, imports can fail if you run the script from a different working directory than expected.

If you want to understand why location matters, read how imports work in Python.

Debugging steps

If the error is still not clear, these checks usually reveal the problem.

Check which Python runs your script

python -c "import sys; print(sys.executable)"

This shows the exact Python interpreter being used.

Check where Python looks for modules

python -c "import sys; print(sys.path)"

This prints the list of folders Python searches for imports.

You can learn more in sys.path explained and the Python sys module overview.

Check which Python pip uses

python -m pip --version

This helps confirm whether pip is connected to the same Python installation.

Check whether the package is installed

python -m pip show package_name

Example:

python -m pip show requests

Test the import directly

python -c "import package_name"

Example:

python -c "import requests"

If this command fails, the package still is not available to that Python interpreter.

Example fixes to show

Example 1: Missing third-party package

Code:

import requests

response = requests.get("https://example.com")
print(response.status_code)

Error:

ImportError: No module named requests

Fix:

python -m pip install requests

Then run the script again.

Example 2: Wrong import name

Incorrect:

import beautifulsoup4

This fails because the import name is not beautifulsoup4.

Correct:

from bs4 import BeautifulSoup

Install with:

python -m pip install beautifulsoup4

Example 3: File named the same as a module

Problem file:

json.py

Problem code:

import json

Fix:

  • Rename json.py to something else
  • Delete __pycache__
  • Run the script again

Example 4: Virtual environment mismatch

You install a package here:

python -m pip install requests

But your editor runs a different interpreter.

Check both:

python -c "import sys; print(sys.executable)"
python -m pip --version

If they point to different Python installations, switch your editor or terminal to the correct environment.

When this is ModuleNotFoundError instead

In modern Python versions, this problem often appears as:

ModuleNotFoundError: No module named 'requests'

ModuleNotFoundError is closely related to ImportError, but it is more specific.

If you see that version of the message, read ModuleNotFoundError: No module named X (Fix).

If your problem is different and Python finds the module but fails to import something from it, see ImportError: cannot import name (Fix).

FAQ

Why do I get this error even after installing the package?

You probably installed it in a different Python version or virtual environment than the one running your script.

Check:

python -c "import sys; print(sys.executable)"
python -m pip --version

Those should match the same Python setup.

What is the difference between ImportError and ModuleNotFoundError?

ModuleNotFoundError is a more specific import error. It is used when Python cannot find the module at all.

ImportError is broader and can happen for other import-related problems too.

Can my file name cause this error?

Yes.

A file such as json.py, sys.py, or requests.py can stop Python from importing the real module.

Rename the file, then remove any __pycache__ folders.

Should I use pip install or python -m pip install?

Use python -m pip install when possible.

It is safer because it uses the selected Python interpreter, which helps avoid environment mismatches.

See also