ModuleNotFoundError What does it mean __main__ is not a package?
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
The error ModuleNotFoundError: __main__ is not a package occurs when you use a relative import (like from .module import something) inside a script that Python is running directly as the entry point. Python treats directly-executed scripts as the __main__ module, which has no package context, so relative imports cannot be resolved. The fix is to restructure your imports to be absolute, or run the module using python -m package.module instead of python module.py.
The Error
Why This Happens
When you run python my_package/main.py, Python sets main.py as the __main__ module. The __main__ module has no parent package, so the dot in from .utils has nowhere to resolve to.
Relative imports use the __package__ attribute to determine the current module's position in the package hierarchy. For directly-executed scripts, __package__ is None, making relative imports impossible.
Fix 1: Use Absolute Imports
Replace relative imports with absolute imports that use the full package path:
Absolute imports are clearer and work regardless of how the script is executed.
Fix 2: Run with python -m
The -m flag tells Python to run the module within its package context, which enables relative imports:
The directory structure must include __init__.py:
Fix 3: Create an External Entry Point
Move the entry point outside the package so all internal imports can be relative:
This is the standard pattern for Python applications. The entry point script imports the package rather than being part of it.
Fix 4: Modify sys.path (Last Resort)
This works but is fragile and not recommended. It hard-codes path assumptions and breaks if the directory structure changes.
Understanding Relative vs Absolute Imports
Relative imports only work when the module has a package context (__package__ is set). This requires either running with -m or importing the module from another script.
The init.py Requirement
Without __init__.py, Python 3 still recognizes namespace packages, but relative imports require regular packages with __init__.py files.
Common Pitfalls
- Running
python file.pyinside a package directory: This executes the file as__main__with no package context. Usepython -m package.modulefrom the parent directory instead. - Missing
__init__.py: Without this file, the directory is not a regular package and relative imports fail. Add an empty__init__.pyto every package directory. - Running from the wrong directory:
python -m my_package.mainmust be run from the directory that containsmy_package/, not from insidemy_package/. - Mixing relative and absolute imports inconsistently: Pick one style per project. Absolute imports are generally preferred because they work regardless of execution context.
- Using relative imports in top-level scripts: If a file is meant to be run directly (like
manage.pyorsetup.py), never use relative imports in it. Only use relative imports in library/package modules.
Summary
- The error occurs because directly-executed scripts have no package context for resolving relative imports
- Use absolute imports (
from package.module import func) for the simplest fix - Use
python -m package.moduleto run scripts with package context, enabling relative imports - Create an external entry point script that imports your package for the cleanest architecture
- Every package directory needs an
__init__.pyfile for relative imports to work
Related reading
- MongoKit vs MongoEngine vs Flask-MongoAlchemy for Flask
- Most efficient way to map function over numpy array
- Most efficient way to reverse a numpy array
- Most idiomatic way to convert None to empty string?
- mongod, mac os x - rlimits warning
- MongoDB - The argument to size must be an Array, but was of type EOO / missing
- Most pythonic way to delete a file which may not exist
- Moving x-axis to the top of a plot in matplotlib
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.