HOWTO · Python

How to Solve Attempted Relative Import With No Known Parent Package in Python

This tutorial describes how to solve ImportError and deal with relative import in Python.

On this page

ImportError: attempted relative import with no known parent package means Python is executing a module without the package context that its leading-dot import needs. The fix is usually to run that module through its package, or to use an absolute import that matches your project layout. Adding __init__.py alone does not give a directly executed script a parent package.

Why Python raises this ImportError

A relative import such as from .helpers import format_name starts from the current module’s package. Python knows that package when the module is imported as part of a package, but a file launched directly is named __main__ and generally has no package name. That is why python path/to/module.py can fail even when the package directory and imported file are present.

The syntax must also be a valid from import. import .module_name is not valid Python syntax; use a form such as from . import module_name or from .module_name import function_name. A leading dot means “relative to this package,” not “look in the same directory regardless of how this file was started.”

Run the module in its package context

For a package named myPackage, place the files under one project directory: IError/myPackage/__init__.py, IError/myPackage/myNewPackage.py, and IError/myPackage/main.py. In main.py, a relative import can be written as from .myNewPackage import createSingleDict. Run it from IError, the directory containing myPackage, with python -m myPackage.main. The -m option asks Python to locate and execute the module as part of its package, so the relative import has a known parent.

Do not launch myPackage/main.py by file path when it relies on a relative import. Also, do not run __init__.py directly: it initializes the package when Python imports the package, and is not the package’s entry point.

Use an absolute import when it fits the project

An absolute import names a module from the import search path, for example from myPackage.myNewPackage import createSingleDict. It can be a good choice when the project already uses package-root imports and the package root is available on sys.path (for example, because you run from the project root or install the package in the environment).

Choose one import style that fits the application and its launch method. Changing to an absolute import does not fix a package name that Python cannot find; check that the named package is importable from the current environment.

Check the package layout and working directory

Confirm that the directory named in the import is the package you intend to load, that the module filename matches the import, and that you launch Python from the expected project root. For the example above, the working directory is IError, not IError/myPackage; the command is python -m myPackage.main. If your package is nested, include each package component in the module path, such as python -m project.tools.runner from the directory containing project.

A regular package commonly includes __init__.py, though Python also supports namespace packages in suitable layouts. The key diagnostic is how the module is executed: a file can live inside a package directory and still lack package context when started directly. Check the active interpreter and environment as well if the package imports differently between a terminal, IDE, and test runner.

Verify the fix with a minimal example

This self-contained example creates a small package in a temporary directory, then starts its entry module with python -m. Run the example with Python; it prints the value returned by the imported function.

from pathlib import Path
import subprocess
import sys
import tempfile

with tempfile.TemporaryDirectory() as temporary_directory:
    project = Path(temporary_directory)
    package = project / "myPackage"
    package.mkdir()
    (package / "__init__.py").write_text("", encoding="utf-8")
    (package / "myNewPackage.py").write_text(
        'def createSingleDict(name, value):\n'
        '    return {"name": name, "value": value}\n',
        encoding="utf-8",
    )
    (package / "main.py").write_text(
        "from .myNewPackage import createSingleDict\n"
        'print(createSingleDict("Jacob", 25))\n',
        encoding="utf-8",
    )
    result = subprocess.run(
        [sys.executable, "-m", "myPackage.main"],
        cwd=project,
        check=True,
        capture_output=True,
        text=True,
    )
    print(result.stdout, end="")

The command’s working directory is the temporary project root, so Python can find myPackage and load main as a package module. If adapting this pattern, keep the module path after -m aligned with the folders under the directory from which you launch Python.

Export submodule names from init.py when needed

Package-level exports are optional convenience. If callers should access selected names as myPackage.createSingleDict, put from .myNewPackage import createSingleDict in __init__.py; importing myPackage then exposes that function. With multiple modules, __init__.py can import selected names from each one, for example from .otherPackage import printName.

These exports control which names are convenient from the package namespace. They do not supply package context to a file launched directly; use package execution or a suitable absolute import to address that cause.