Python
Module Import Error
Troubleshooting
Programming
Python Modules

Unable to import a module that is definitely installed

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

When Python says a module cannot be imported even though it is "definitely installed," the problem is usually not installation in the abstract. It is almost always an environment mismatch, path problem, or name collision. In other words, the package exists somewhere, but not in the interpreter context that is actually running your code.

First Check Which Python You Are Using

The most common cause is that pip and python are pointing at different environments.

Run these commands:

bash
1which python
2which pip
3python -m pip --version
4python --version

The key point is this: python -m pip tells you which pip belongs to the interpreter you are about to use. That is much more reliable than trusting a bare pip command on a machine with multiple Python versions or virtual environments.

Virtual Environment Mismatches

A package may be installed in one environment but imported from another.

Typical scenario:

  • package installed in the global interpreter
  • script run inside a virtual environment
  • or the reverse

Inside the environment you actually use, install the package like this:

bash
python -m pip install requests

Then verify:

bash
python -c "import requests; print(requests.__file__)"

If that works, the environment mismatch was the issue.

Local File Name Collisions

Another very common cause is accidental shadowing. If your project contains a file named like the package, Python may import that file instead of the real installed module.

Example bad layout:

text
project/
  requests.py

Then this code breaks in confusing ways:

python
import requests
print(requests)

Because Python may be loading the local requests.py instead of the real third-party package.

Also watch for stale __pycache__ directories after renaming such files.

Inspect sys.path

Python imports by searching the directories listed in sys.path.

python
1import sys
2
3for path in sys.path:
4    print(path)

This helps answer whether the directory that contains the installed module is even visible to the running interpreter.

If your package is in a custom location, the issue may be path configuration rather than installation.

Check Where the Module Actually Resides

You can ask Python directly:

python
1import importlib.util
2
3spec = importlib.util.find_spec("requests")
4print(spec)

If the result is None, the current interpreter cannot see the package at all.

If it points to an unexpected path, you may have a naming conflict or a surprising environment setup.

Version and Compatibility Problems

Sometimes the package is installed but still unavailable because it was installed for a different Python version.

Examples:

  • installed under Python 3.11, code running under Python 3.9
  • installed in a Conda environment, code running from system Python
  • installed for one architecture or build target, then run elsewhere

That is why checking python --version and python -m pip --version together is so important.

Jupyter and IDEs Add Another Layer

Notebook kernels and IDE run configurations often use a different interpreter than your terminal.

So a package can import correctly in the shell but fail in:

  • Jupyter notebook
  • VS Code selected interpreter
  • PyCharm project interpreter
  • task runner inside CI

In those cases, fix the interpreter selection first. Reinstalling the package repeatedly in random environments does not solve the root cause.

A Useful Diagnostic Snippet

python
1import sys
2import importlib.util
3
4print(sys.executable)
5print(sys.version)
6print(importlib.util.find_spec("requests"))

This gives you three immediate clues:

  • which interpreter is running
  • which Python version it is
  • whether the module is visible to that interpreter

Common Pitfalls

  • Using pip install with one interpreter and running code with another.
  • Shadowing a package name with a local file such as requests.py.
  • Forgetting that virtual environments, Conda envs, Jupyter kernels, and IDE interpreters can all differ.
  • Looking only at whether the package is installed somewhere rather than whether it is installed for this interpreter.
  • Debugging import syntax before checking sys.executable and python -m pip --version.

Summary

  • A module can be installed and still be invisible to the interpreter you are using.
  • Start by checking python, pip, and python -m pip alignment.
  • Watch for local file name collisions and path problems.
  • Inspect sys.path, sys.executable, and importlib.util.find_spec to see what Python can actually resolve.
  • Most "definitely installed" import failures are really environment-selection failures.

Course illustration
Course illustration

All Rights Reserved.