ModuleNotFoundError: Relative Import in Non-Package

ModuleNotFoundError: No module named '__main__.utils'; '__main__' is not a package

Quick Answer

You are using a relative import in a script run directly. Relative imports only work in packages. Run as a module with python -m or use absolute imports.

Why This Happens

When you run python script.py, Python sets __name__ to '__main__' and does not consider the file part of a package. Relative imports fail because there is no package context.

The Problem

# project/app/main.py
from .utils import helper
# Running: python project/app/main.py

The Fix

# Run as module: python -m app.main
# Or use absolute import:
from app.utils import helper

Step-by-Step Fix

  1. 1

    Run as module

    Use python -m package.module from the project root.

  2. 2

    Use absolute imports

    Replace relative with absolute imports.

  3. 3

    Check __init__.py files

    Ensure all directories in the import path have __init__.py.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free