Register Login

How to fix the ImportError: attempted relative import with no known parent package in python

Updated Oct 09, 2022

Every program in any programming language starts with a header file, package, or module. In Python, the Import statement is the first statement of the program that allows users to import modules into the code. It has a similar function to the #include header_file statement of C or C++.

Python modules can access the built-in code from other modules by importing the Python function or file using the word "import." But most Python users do not know that their program may show an error if they fail to use the packages in a proper format.

This article will discuss one of the most common error messages, i.e., the ImportError: attempted relative import with no known parent package in Python.

What is import statement in Python?

One of the most common ways to invoke import machinery is to use the import statement, but apart from this, users can also add to the Python import. The import statement comprises the import keyword, including the name of a Python module.

In the import statement, the interpreter performs two operations, i.e., it involves two procedures. Firstly, import looks for a module, and secondly, the import wraps the search result to the name in the local scope.

When users import a module in their code, Python executes all of the code of the desired module file and presents it to the importer file to run.

Syntax of an import statement in Python:

The import statement allows programmers to import both packages and modules. Generally, Python has two types of import syntax. When programmers use the first import syntax, they import the built-in resource directly. Programmers can also import specific objects from the module or package.

Note:

It is a point to remember that when users import a package, it virtually imports the package's __init__.py file as a module.

import abcd

Here, "abcd" can either be a package or a module. Again, when users use the second syntax, i.e., they can also import the resource from other packages or modules.

Syntax:

from abcd import mod

here, the "mod" can be a subpackage, module, or object, like a class or function.

What are packages and modules in Python?

Before understanding this error, we have to understand the concept of packages & modules so that we can easily debug those errors. When users use a package in a Python code, they import the file __init__.py, which is for every user-oriented code.

A Python package operates on a library, explaining the whole Python code as a single unit of a particular function. A Python package also changes the user-interpreted code by modifying it so that the Python interpreter can efficiently operate it in the run time.

On the other hand, a Python module is a file including Python code in run time for any user-oriented code. The Python modules are different libraries themselves, which contain built-in functions.

Syntax:

from abcd import mod

Here, the term "abcd" signifies a package, and the term "mod" defines a module or a sub package.

The cause behind the error named, ImportError: attempted relative import with no known parent package in python:

Before fixing the error, let us know why the Python interpreter shows the Importerror message. as we have discussed above, users can import a module inside a package, two packages, or one module inside another module.

Often, programmers make a mistake while calling a package in the import statement. They should know the difference between the parent package and the subpackage while importing.

If users fail to import the parent library in the import statement, the interpreter will show the error message, "ImportError: attempted relative import with no known parent package in Python."

How to fix the "Importerror attempted relative import with no known parent package" error in Python?

The solution to this problem is easy. Before advancing to the body of the Python program, programmers first create a Python file with their setup name; then make that package global so they can easily access it.

After users create their file, inside that file, they need to specify the name of the Python package they want to import.

Let us see an example with the help of a code and understand it more precisely:

from setuptools import setup, find_packages
setup(name = 'package_two', packages = find_packages())

Here, you can see that we have provided our package name. Now we need to remove the relative path from our one.py file.

To do so follow the below code example:

Code Snippet:

from package_two import second_module
print("Hello World!")
two.fun()

Output:

Explanation:

Now, we can see in the above output there is no error message. Both the parent package and the module are known to the interpreter.

An example to show how to use the Python packages and modules in the right format:

└── myproject
├── first_package
│ ├── A.py
└── second_package
├── B.py
├── C.py
└── sub_package
└── D.py

Conclusion:

We hope from this article; Python programmers get the solution to fix the error named "ImportError: attempted relative import with no known parent package." Debugging these import-related errors is essential in the real-life scenarios where software development requires using packages & modules frequently to reduce or eliminate repetitive work in Python prorgrams.


×