Can't import my own modules in Python
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 ModuleNotFoundError: No module named 'mymodule', it means mymodule.py is not in any directory Python is searching. Python looks in the current directory, installed packages, and directories listed in sys.path. The fix is usually one of: running from the correct directory, adding an __init__.py to make a package, installing your project with pip install -e ., or adjusting sys.path.
How Python Finds Modules
When you write import mymodule, Python searches these locations in order:
The empty string '' means the current working directory (where you ran python), not the directory of the script file.
Fix 1: Run from the Correct Directory
Fix 2: Create a Package with init.py
__init__.py can be empty — its presence tells Python that the directory is a package. Without it, Python treats the directory as a regular folder and cannot import from it.
Fix 3: Use Relative Imports (Within a Package)
Relative imports use dots: . for the current package, .. for the parent. They only work inside packages (directories with __init__.py), not in standalone scripts.
Fix 4: Install Your Project with pip install -e
The best approach for projects with multiple modules:
Or with pyproject.toml:
Fix 5: Add to sys.path (Quick Fix)
Or set the PYTHONPATH environment variable:
This works but is fragile. Prefer pip install -e . for anything beyond quick scripts.
Fix 6: Use python -m to Run Modules
python -m adds the current directory to sys.path and treats the argument as a module path, which correctly resolves package imports.
Debugging Import Problems
Common Project Structures
Common Pitfalls
- Naming conflicts: If your module is named
email.pyorjson.py, Python imports the standard library module instead of yours. Never name your files after standard library modules. - Missing
__init__.py: Without__init__.py, a directory is not a package. Python 3 has "namespace packages" (implicit packages without__init__.py), but they behave differently and can cause confusion. - Running from the wrong directory:
sys.path[0]is the directory of the script being run, not the directory you typedcdinto. Usepython -morpip install -e .to avoid this. - Virtual environment not activated: If you installed your module in a virtualenv but forgot to activate it, Python uses the system Python which does not have your module.
- Circular imports: Module A imports Module B, which imports Module A. Python raises
ImportErrororAttributeErroron the partially-loaded module. Break the cycle by moving the import inside a function or restructuring the code.
Summary
- Python searches
sys.pathfor modules — the current directory is included by default - Create
__init__.pyfiles to make directories into importable packages - Use
pip install -e .to install your project in development mode (best approach) - Use
python -m mypackage.moduleinstead ofpython mypackage/module.py - Avoid naming files after standard library modules (no
json.py,email.py, etc.) - Debug with
sys.path,importlib.util.find_spec(), andmodule.__file__

