ImportError No module named when trying to run Python script
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
ImportError or ModuleNotFoundError usually means Python could not find the module where it expected to. The fix is not always “install the package.” The real cause may be the active interpreter, the current working directory, the package layout, or the way the script was launched.
Check Which Python You Are Running
Start by confirming the interpreter and environment actually executing the script.
Those commands tell you whether pip is installing into the same interpreter that runs the script. A very common failure mode is installing a package into one virtual environment and executing the script with another Python binary.
Confirm the Module Is Installed
If the missing import is a third-party package, check whether it exists in the current environment.
If nothing appears, install it with the same interpreter:
Using python -m pip is safer than calling pip directly because it ties installation to the interpreter you chose.
Understand How Python Builds sys.path
Python searches imports using sys.path. That includes the script directory, standard library locations, and installed site-packages directories.
If your project package is not on that list, Python will not find it. That often happens when a script is launched from the wrong directory or when package code is run as a loose file instead of as a module.
Prefer python -m for Package Code
Suppose your project looks like this:
If main.py imports app.utils, running the file directly from inside app can break imports. Instead, run it from the project root:
That tells Python to treat app as a package and build the import path correctly.
Watch for Naming Conflicts
Local filenames can shadow installed packages or standard-library modules. For example, if your file is named requests.py, then this import becomes confusing:
Python may import your local file instead of the external package. Similar issues happen with names such as json.py, typing.py, or random.py. Rename the conflicting file and remove any leftover __pycache__ directories if necessary.
Relative Imports Require Package Context
Inside package code, relative imports work only when Python knows the package context.
If you run that file directly as a script, Python does not treat it as part of a package, so the relative import fails. Again, the fix is usually to run the package entry point with python -m package.module.
Virtual Environments Solve Many Cases
A clean virtual environment reduces ambiguity.
That makes the interpreter, installed packages, and project dependencies much more predictable.
A Practical Debug Checklist
When an import fails, this short script is often enough to reveal the problem.
Run it with the same command that triggers the error. You will usually spot either the wrong interpreter or a missing project path immediately.
Common Pitfalls
The most common mistake is installing a package with one interpreter and running the script with another. Another is launching a file inside a package directly instead of using python -m. Developers also frequently shadow real packages with local filenames such as requests.py. Finally, adding random paths to sys.path can hide the real packaging problem instead of fixing it cleanly.
Summary
- Verify the exact Python interpreter running the script.
- Install packages with
python -m pipto match that interpreter. - Inspect
sys.pathwhen Python cannot find local project modules. - Run package code with
python -m package.module. - Avoid local filenames that shadow standard or third-party modules.

