Importerror attempted relative import with no known parent package

Importerror attempted relative import with no known parent package

ImportError attempted relative import with no known parent package error occurs when we import any module from any package (Directory) where __init__.py file is missing or the path for the package is undefined. In this article, We will practically fix this problem.

ImportError attempted relative import with no known parent package ( Cause ) –

In order to understand the cause let’s take a scenario for example. Suppose this is the file structure –

import with no known parent package example scenario
import with no known parent package example scenario

 

As you can see, we are having two packages –

package_one -> script.py

Here is the code inside script.py-

from .package_two import functionality
print("Running Script file")
functionality.execute()

package_two -> functionality.py

## functionality.py file
def execute():
    print("Running functionality")

Now you can see that we are importing package_two module functionality.py in the script.py file of package_one.

import with no known parent package explanation
import with no known parent package explanation

 

Relative Import –

While importing we use (.) dot before as you can see below image. This makes it a relative import. Let me explain since we are calling functionality modules from script.py files which are belonging to different packages( directories). Hence the caller file (script.py ) has to go one level up which is why one (.) dot. If suppose there are more directory levels we can use multiple dots for the same.

This is called relative importing while absolute importing needs a full path for the same.

Let’s run and see the error-

 

Importerror attempted relative import with no known parent package
Importerror attempted relative import with no known parent package

since we do not have a parent package define.

 

ImportError attempted relative import with no known parent package ( Solution) –

Solution 1 : Setup File Creation –

The easiest way to fix this relative import error is using the setup.py file, we can make the respective package global. Here are the steps-

Step 1: Create setup.py file-

All you need to create a simple python file with a setup name. Here we will define which package we want to make global.

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

In the above example, We are importing module from package_two.

Step 2: Running setup.py file –

Use the below command –

python setup.py install
setup file
setup file

Step 3: Modifying caller script –

We need to call this script.py file but as we have made this package global so need to remove this (.) in importing statement.

from package_two import functionality
print("Running Script file")
functionality.execute()

Now, let’s run this script.py file.

import with no known parent package solved
import with no known parent package solved

Hey !! we have cracked the same.

Solution 2 : package path update in system path –

As an alternative to this approach, we can also move the relevant package to the directory where we have a path set.

Or we can copy the same package to any existing directory for which path is already configured.

In order to set the path for the package, We can use sys and pathlib modules.

sys path management
sys path management

 

Solution 3 : Using Absolute Path –

If we are really confused about relative path import then you should change the import statement such that every import is an absolute path. See when we install Python and configure its path to a system of OS. Python always looks for the module from that root path if we mention the same level path in the import statement then it will be a full absolute path but it will be lengthy in nature. This is the only drawback of this approach.

Other Questions

What is the use of __init__.py file ?

The __init__.py file allows the developer to use the directory as a Python package. The file can be empty or contain some specific lines of code. When you create a __init__.py file inside the directory you are telling to the Python interpreter to treat that directory as a package. The uses of this file are Package Initialization, Controlling Import Behavior, and Package-Level Coding.

Read more in detail about ImportErrors in the below well-written and informative article.

Easiest way to Fix importerror in python ( All in One )

Difference Between a Module and a package in Python?

The module is a single Python file with multiple methods to achieve a specific task.  However, a package is a collection of modules to achieve a specific set of functionalities. Just Like Numpy, Pandas, etc. These are packages in Python.

 

Thanks 

Data Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages Datasciencelearner.com, where he and his team share knowledge and help others learn more about data science.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner