Skip to content
DevOps AI ToolKit
Newsletter
All guides
AI for Bash & Python Automation By James Joyner IV · · 8 min read Last reviewed Jul 2026

Python Error Guide: 'ImportError: attempted relative import with no known parent package' — Cause, Fix, and Troubleshooting Guide

Quick answer

Fix Python 'attempted relative import with no known parent package': run modules with python -m, add __init__.py, or switch to absolute imports.

  • #python
  • #automation
  • #troubleshooting
  • #errors
Free toolkit

Stuck on this Bash & Python Automation error? Get the free incident triage checklist

A one-page PDF — the exact steps to isolate, fix, and verify a production error like this one. No spam, unsubscribe anytime.

Overview

Python raises this when a module using a relative import (from . import x) is run in a way that gives it no parent package:

ImportError: attempted relative import with no known parent package

Relative imports resolve against the module’s __package__ — its position inside a package hierarchy. When you run a file directly (python3 app/main.py), Python executes it as the top-level __main__ module with no package context, so from . import helpers has no . to resolve against. The fix is to run it as part of a package (python3 -m app.main) or use absolute imports, not to keep running the file by path.

Symptoms

  • The error fires only when running a file directly (python3 pkg/module.py), not when importing it.
  • python3 -m pkg.module runs the same code without the error.
  • It appears after moving code into a package directory and adding from . import ....
  • A SystemError/ImportError variant mentions no known parent package or beyond top-level package.

Common Root Causes

  • Running a package module by file pathpython3 app/main.py executes as __main__ with no package, so relative imports fail.
  • Missing __init__.py in older setups where the directory isn’t recognized as a package (namespace packages relax this, but entry-point execution still needs -m).
  • sys.path pointing inside the package rather than at its parent, so the package name isn’t importable.
  • A script that is genuinely standalone using relative-import syntax it shouldn’t.
  • Going above the top-level package — too many leading dots (from ...pkg import x) for the current depth.

Diagnostic Workflow

See how the module perceives its own package context:

# add temporarily near the failing import
print("__name__:", __name__, "__package__:", __package__)

If __package__ is empty/None and __name__ is __main__, you’re running by path — that’s the cause. Confirm the package is importable from the project root:

cd /path/to/project-root
python3 -c "import app.main"      # works if the package layout is correct

Run it the intended way:

python3 -m app.main              # gives main.py its package context

Example Root Cause Analysis

A CLI packaged under app/ broke when run the old way:

app/cli.py:
    from .config import settings

$ python3 app/cli.py
ImportError: attempted relative import with no known parent package

Executed by path, cli.py ran as __main__ with no parent package, so .config had nothing to resolve against. Adding a debug print confirmed __package__ was empty. Two correct fixes:

Run it as a module from the project root (preferred — keeps the relative imports):

cd /path/to/project
python3 -m app.cli

Or, for a real entry point, define one in packaging and let it invoke a function, so users never run the file by path:

# pyproject.toml
[project.scripts]
mytool = "app.cli:main"
pip install -e .
mytool          # runs app.cli:main with full package context

If the file must be runnable standalone, switch its relative imports to absolute (from app.config import settings) and ensure the project root is on sys.path.

Prevention Best Practices

  • Run package modules with python3 -m pkg.module, never by file path, so relative imports resolve.
  • Define console-script entry points in pyproject.toml ([project.scripts]) so users invoke a command, not a path.
  • Prefer absolute imports (from app.config import settings) in application code — they work regardless of how the module is launched.
  • Keep __init__.py in package directories for clarity and predictable behavior, even with namespace packages.
  • Structure projects with a clear root on sys.path (installed with pip install -e .), so package names are importable.
  • Reserve relative imports for genuine intra-package references, and always run such modules via -m or an entry point.

Quick Command Reference

python3 -m app.cli               # run as a module: relative imports work
python3 -c "import app.cli"      # verify the package is importable
pip install -e .                 # install so entry points and imports resolve
# debug the context near the failing import:
python3 -c "import app.cli"      # prints __package__ if you added the probe
print(__name__, __package__)     # __main__ + empty -> run by path (the cause)
from app.config import settings  # absolute import: launch-method independent

Conclusion

ImportError: attempted relative import with no known parent package means a module using from . import ... was run without a package context — almost always because it was launched by file path as __main__. Run package modules with python3 -m pkg.module, define proper console-script entry points for user-facing tools, and prefer absolute imports in application code. Structure the project so its root is importable and this error stops recurring.

Free download · 368-page PDF

Fixed it? Get 500 Bash & Python Automation & DevOps AI prompts — free

500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.

  • 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
  • Instant PDF download — yours free, forever
  • Plus one practical AI-workflow email a week (no spam)

Single opt-in · unsubscribe anytime · no spam.

Did this fix your issue?

Free download · 368-page PDF

Get 500 Battle-Tested DevOps AI Prompts — Free

500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.

  • 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
  • Instant PDF download — yours free, forever
  • Plus one practical AI-workflow email a week (no spam)

Single opt-in · unsubscribe anytime · no spam.