ImportError: attempted relative import with no known parent package in Python 3

Resolve problem like a hacker

Fullstack CTO
4 min readFeb 17, 2023

The “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.x, and is related to changes in the way that relative imports are handled.

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 named “init.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.

Variants for resolving this problem

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:

Example 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.

Example 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.

Resolve the problem like a hacker

If you’re encountering the “attempted relative import with no known parent package” error 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:

Step 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.

Step 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.

Step 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 in to 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.

--

--

Fullstack CTO
Fullstack CTO

Written by Fullstack CTO

CTO and co-founder at NEWHR & Geekjob

Responses (1)