When working with Python projects you might come across a frustrating error ImportError attempted relative import with no known parent package.
This error typically occurs when Python encounters an issue with relative imports in packages or modules and it’s a common hurdle for both beginner and experienced developers.
In this blog post we’ll dive deep into the causes of this error explore different methods for resolving it and provide tips for preventing it from reoccurring in the future.
What Does ImportError: Attempted Relative Import with No Known Parent Package Mean?

This error occurs when you try to use a relative import in a Python script that’s being executed directly rather than as part of a package. In Python, relative imports allow you to import modules within the same package without specifying their full paths.
However, Python needs to know the context of the package to resolve these imports and when it doesn’t it throws the ImportError: attempted relative import with no known parent package.
For example, suppose you have the following directory structure:

In module2.py
, you might try to import module1.py
with a relative import like this:

If you try running module2.py
directly as a standalone script (for example, by running python module2.py
), Python doesn’t know what the parent package is and you’ll encounter the ImportError: attempted relative import with no known parent package.
Why Does This Error Occur?
To understand why this error occurs, let’s break it down:
Relative Imports Require a Package Context
Python uses relative imports to allow modules within the same package to import each other using a shorthand. However relative imports only work when Python knows the context of the package.
If you’re running a module directly Python doesn’t consider it as part of a package and thus can’t resolve relative imports.
Running a Module Directly vs. Running as a Package:
The issue arises when you’re trying to run a script inside a package. If you run a script inside a package directly, Python treats it as a top-level script and doesn’t consider the parent package, which breaks relative imports.
The Role of __main__
:
In Python, if you run a module as a script the __name__ variable is set to "__main__"
. This indicates that the module is the entry point and Python doesn’t consider it part of a larger package which is necessary for relative imports to work.
How to Fix the “ImportError: Attempted Relative Import with No Known Parent Package”

Now that we understand why this error happens let’s explore different ways to fix it.
Run the Package as a Whole (Avoid Running Modules Directly)
The most straightforward way to fix this issue is by ensuring that you don’t run individual modules directly if they are part of a package. Instead run the top-level package.
For example, instead of running module2.py
directly:

Run the package like this:

Here, Python recognizes that module2
is part of the package
and relative imports will work correctly.
Use Absolute Imports Instead of Relative Imports
Another solution is to use absolute imports instead of relative imports. Absolute imports explicitly reference the full module path, making them independent of the module’s location.
For example, instead of:

You can use:

This will work even if you run the module directly as Python doesn’t rely on the parent package for absolute imports.
Modify the PYTHONPATH
to Include the Parent Directory
If you’re working in a development environment where you need to run individual modules directly, another approach is to modify the PYTHONPATH
to include the parent directory of your package. This can be done in your terminal session like so:

This ensures that Python can find the package and resolve the relative imports correctly even if you’re running a module directly.
Using __package__ to Fix the Import Context
In some cases, setting the __package__
variable manually can help resolve relative import issues. For example, you can add the following code to the top of your script to set the __package__
:

This explicitly sets the package context, allowing relative imports to work even when the module is run directly.
Additional Related Errors
You might also encounter other similar import-related issues when working with Python, such as:
ImportError: Attempted Relative Import Beyond Top-Level Package
This error occurs when a relative import tries to go beyond the top-level package. For instance, attempting to use from import module
inside a module at the top level of a package will trigger this error as Python doesn’t support going up from the top-level package.
ImportError in VSCode
If you’re using Visual Studio Code (VSCode) you might encounter the ImportError: attempted relative import with no known parent package error due to the IDE not setting the correct context.
You can resolve this by configuring VSCode to run your scripts as part of the package or by modifying the launch configuration.
Jupyter Notebook ImportError
Attempted Relative Import with No Known Parent Package: This error can also occur when using Jupyter Notebooks.
Since Jupyter runs cells as individual scripts, it might not recognize the package structure. A workaround is to either use absolute imports or run the Jupyter Notebook from the root directory of the package.
Conclusion
The ImportError: attempted relative import with no known parent package can be a frustrating roadblock, but understanding the underlying cause and applying the right fixes can help you overcome it.
Whether you choose to run your package as a whole, switch to absolute imports, or adjust your development environment settings, resolving this issue will improve your Python coding experience.
If you continue to run into issues, consider exploring Python documentation on imports or checking your IDE settings to ensure that your package structure is correctly recognized.
FAQs
What is the ImportError: attempted relative import with no known parent package?
This error occurs when you try to use relative imports in a module that is run directly causing Python to not recognize the parent package.
How can I fix the “ImportError” in Python?
You can fix it by running the module as part of the package with the -m
flag or using absolute imports instead of relative ones.
What is the difference between relative and absolute imports in Python?
Relative imports are shorthand to import modules within the same package, while absolute imports specify the full path to the module.
Can I use relative imports in a script run directly?
No, relative imports only work when the module is part of a package. Running a module directly breaks the import context.
What happens if I use relative imports in Jupyter Notebook?
Relative imports can fail in Jupyter Notebooks because they run cells as standalone scripts, lacking package context.
How do I avoid this error in VSCode?
Ensure your Python scripts are part of a package or configure VSCode to recognize the package structure correctly.
How can I make Python recognize the parent package?
You can set the PYTHONPATH
environment variable to include the parent directory or run the package using python -m
.
What should I do if I encounter “ImportError: attempted relative import beyond top-level package”?
This error happens when a relative import tries to go beyond the top-level package. To fix it, restructure your imports or use absolute imports.