How to Fix: ImportError: attempted relative import with no known parent package
Last Updated :
02 Apr, 2025
ImportError: attempted relative import with no known parent package error occurs when attempting to import a module or package using a relative import syntax, but Python is unable to identify the parent package. In this article, we will see how to solve this error in Python.
What is ImportError: Attempted Relative Import With No Known Parent Package?
The "ImportError: attempted relative import with no known parent package" error typically arises in Python projects with multiple modules or packages. Python uses the concept of packages and modules to organize code into manageable units. When attempting to perform a relative import, Python relies on the presence of a parent package to resolve the import statement. However, if Python cannot identify the parent package, it raises the "ImportError."
Error Syntax:
ImportError: attempted relative import with no known parent package
Below are some of the reasons why ImportError: attempted relative import with no known parent package occurs in Python:
- Incorrect package structure
- Missing __init__.py file
Incorrect Package Structure
One common reason for this error is an incorrect or inconsistent package structure. Python expects a specific structure to recognize packages and modules correctly. If the package structure deviates from this standard, Python may fail to identify the parent package.
Example of Incorrect Structure:
my_package/
│── module.py
│── main.py
module.py:
Python
def hello():
print("Hello from module.py")
main.py:
Python
from .module import hello # Relative import
def main():
hello()
if __name__ == "__main__":
main()
Output

Missing __init__.py File
Common cause of this error is a missing __init__.py file in the package directory. This file tells Python that the directory should be treated as a package. Without __init__.py, Python will not recognize the folder as a package, leading to the ImportError.
Example of Missing __init__.py File
folder/
│── main.py
│── utils.py (Missing `__init__.py` file)
main.py:
Python
from .utils import some_function
def main():
some_function()
if __name__ == "__main__":
main()
Output

Solution: ImportError: Attempted Relative Import With No Known Parent Package
Below are the solution for ImportError: Attempted Relative Import With No Known Parent Package in Python:
- Correct Package Structure
- Create __init__.py File
Correct Package Structure
To fix the error, remove relative imports when running the script directly, or execute the script using the -m flag.
Corrected main.py
Python
from module import hello # Use absolute import instead of relative
def main():
hello()
if __name__ == "__main__":
main()
Output
Hello from module.py
Create __init__.py File
To ensure Python recognizes the package, create an __init__.py file in the directory.
folder/
│── __init__.py # This file is required for Python to recognize the package
│── utils.py
│── main.py
utils.py:
Python
def some_function():
print("This is some_function() from utils.py")
main.py:
Python
from utils import some_function # Absolute import
def main():
some_function()
if __name__ == "__main__":
main()
Output
This is some_function() from utils.py