Python ImportError: Attempted Relative Import With No Known Parent Package Solved

The “ImportError: attempted relative import with no known parent package” occurs when a relative import in a module has no parent package. Here’s how to solve it.

Written by Alexander Majorov
Published on Jul. 22, 2025
Two developers reviewing an error in code
Image: Shutterstock / Built In
Brand Studio Logo
Summary: The “ImportError: attempted relative import with no known parent package” occurs in Python 3 when using a relative import without a recognized parent package. It can be fixed by using absolute imports, converting the module to a package or installing it with a setup.py.

The ImportError: attempted relative import with no known parent package error occurs when you are trying to use a relative import in a module that has no parent package. This error commonly occurs in Python 3 and beyond, and is related to changes in the way that relative imports are handled.

Python ImportError: Attempted Relative Import With no Known Parent Package Explained

The ImportError: attempted relative import with no known parent package signals that the relative import in a module has no parent package. It often is related to changes in the way relative imports are handled in Python 3 and beyond. 

 

How to Solve ImportError: attempted relative import with no known parent package

To fix this error, you can use one of the following approaches:

1. Change the Import Statement to an Absolute Import

Instead of using a relative import, use an absolute import by providing the full package path. For example, if your module name is mymodule and it is located in a package called mypackage, you can replace the relative import statement from . import mymodule with from mypackage import mymodule.

2. Convert Your Module to a Package

To convert your module to a package, create an empty file namedinit.py in the same directory as your module. This file is required to make the directory a package and should contain any initialization code you need for your package.

Once you have made the necessary changes, you should be able to import the module without encountering the “attempted relative import with no known parent package” error.

More on PythonHow to Fix KeyError Exceptions in Python

 

ImportError: Attempted Relative Import With no Known Parent Package Example Solutions

In Python 3.X, the way that relative imports are handled has changed, which can lead to the attempted relative import with no known parent package error. This error occurs when a module attempts to perform a relative import, but there is no known parent package to use as a reference.

Here are some examples of how to fix this error:

1. Changing the Import Statement to an Absolute Import

If you’re getting the attempted relative import with no known parent package error because you’re using a relative import, you can fix the error by changing the import statement to an absolute import. Here’s an example:

Suppose you have a package called mypackage with a module called mymodule that you’re trying to import. You might have code that looks like this:

from . import mymodule

This code is using a relative import to import mymodule. However, if mypackage is not recognized as the parent package, you’ll get the attempted relative import with no known parent package error. To fix this, you can use an absolute import by providing the full package path:

from mypackage import mymodule

This code imports mymodule using an absolute import. Because the full package path is provided, there is no need to reference the parent package, and the error should be resolved.

2. Converting Your Module to a Package

If you’re getting the attempted relative import with no known parent package error because your module doesn’t have a parent package, you can fix the error by converting your module to a package. Here’s an example:

Suppose you have a module called mymodule that you’re trying to import. However, the module is not located in a package and does not have a parent package. You might have code that looks like this:

from . import mymodule

This code is using a relative import to import mymodule. However, because mymodule doesn’t have a parent package, you’ll get the attempted relative import with no known parent package error. To fix this, you can convert your module to a package by creating an empty file named init.py in the same directory as your module:

mymodule/
├── __init__.py
└── mymodule.py

This file is required to make the directory a package and should contain any initialization code you need for your package. Once you’ve created this file, you can update your import statement to use an absolute import:

from mymodule import some_function

This code imports some_function using an absolute import. Because mymodule is now a package with a parent package, there is no need to reference the parent package, and the error should be resolved.

By following these examples, you should be able to fix the attempted relative import with no known parent package error in your Python code.

A tutorial on how to resolve absolute and relative ImportErrors in Python. | Video: chinamatt

More on PythonHow to Fix Error: Externally Managed Environment in Pip

 

Other Ways to Solve ‘ImportError: Attempted Relative Import With no Known Parent Package’

If you’re encountering the attempted relative import with no known parent package error message in your Python code, there are several ways to fix it. One way is to use a setup.py file to make the corresponding package global, which can be done by following these steps:

1.  Create a setup.py File

First, create a simple Python file named setup.py. In this file, specify which package you want to make global. For example:

from setuptools import setup, find_packages

setup(name='mymodule', packages=find_packages())

In the above example, we import the module from package_two.

2. Run the setup.py File

Run the setup.py file using the following command:

python setup.py install

This will install the package and make it globally available.

3. Change the Calling Script

In your calling script, update the import statement to remove the dot (.) to reflect that the package is now global. For example:

from mymodule import functionality

...

Now, when you run the script file, the error should be resolved.

As an alternative to this approach, you can also move the package into a directory where the path is set, or copy the same package to any existing directory for which the path is already configured. To set the path for the package, you can use the sys and pathlib modules.

Frequently Asked Questions

The ImportError: attempted relative import with no known parent package is thrown in Python when you use a relative import module that has no known parent package. This occurs most frequently in Python 3.X due to changes in how relative imports are handled.  

There are two common ways to fix the error:

  1. Change the import statement to an absolute import.
  2. Convert the module to a package.
Explore Job Matches.