How To Fix ImportError: Attempted Relative Import With No Known Parent Package (Python)?

How to Fix ImportError: Attempted Relative Import With No Known Parent Package (Python)

Have you come across an import error that you were not able to fix? Well, In this article, let us see how to fix ImportError: Attempted Relative Import With No Known Parent Package (Python). 

One of the most well-known and commonly utilized programming languages today is Python. Python is used for data analytics, machine learning, and even design in addition to web and software development. So many software developers personally show interest in learning the language. Like any other programming language, python also throws errors when the code is not compiled properly.

To fix ImportError: Attempted Relative Import With No Known Parent Package (Python), Use the setup.py file, to make the necessary package global, by creating the setup.py file, running the setup.py file, and finally changing the caller script.

Continue reading further to understand in detail how to fix ImportError: Attempted Relative Import With No Known Parent Package (Python).

How To Fix ImportError: Attempted Relative Import With No Known Parent Package (Python)?

To fix ImportError: Attempted Relative Import With No Known Parent Package (Python), Use the setup.py file, to make the necessary package global, by creating the setup.py file, running the setup.py file, and finally changing the caller script.

By utilizing the word “import,” Python modules can import a Python function or file to access the built-in code from other modules. However, the majority of Python users are unaware that using the packages improperly could result in an error in their code. Programmers are able to import both packages and modules using the import statement. Python typically offers two different import syntaxes. Programmers import the built-in resource directly when they employ the initial import syntax. Additionally, programmers can import particular objects from a module or package.

We come across the ImportError: Attempted Relative Import With No Known Parent Package (Python) because

  1. __init .py files are missing when we import a module from a package (Directory).
  2. The package’s path is not known.

Also Read: How do I convert a list to a string in Python?

Consider we have two packages: script.py and functionality.py

1. package_one -> script.py

from .package_two import functionality

print(“Running Script file”)

functionality.execute()

2. package_two -> functionality.py

## functionality.py file

def execute():

print(“Running functionality”)

Now, you must use relative import by importing the functionality.py module from package two into the script.py file of package one, by using a dot before (.) as we are using functionality modules from script.py files that are contained in various packages. Because of this, the caller file (script.py) must move one level up, hence the one (.) dot. We can use many dots for the same if, for example, there are additional directory levels.

How to Fix ImportError: Attempted Relative Import With No Known Parent Package (Python) - error

Now, execute the code and we will get the Import error because we cannot define a parent package and we need to fix ImportError: Attempted Relative Import With No Known Parent Package (Python) Using the setup.py file, we can make the necessary package global, which is the simplest solution to resolve this relative import error.

Also Read: How To Make A Discord Bot Using Python And Discord API

How To Create A Setup.Py File?

All you need to do is create a straightforward setup-named Python file. Which package we wish to make global will be specified here.

from setuptools import setup, find_packages  

setup(name = ‘package_two’, packages = find_packages())

How To Execute The Setup.py File?

python setup.py install

How To Change The Caller Script?

The script.py file must be called, but since the package has been declared global, the (.) must be removed from the importing declaration.

from package_two import functionality

print(“Running Script file”)

functionality.execute()

Finally, execute the script.py file and it will finish executing successfully and it will fix ImportError: Attempted Relative Import With No Known Parent Package (Python).

By following this process, you easily get rid of the import error and fix ImportError: Attempted Relative Import With No Known Parent Package (Python).

Wrapping Up

We hope this article has given you a clear explanation of how to fix ImportError: Attempted Relative Import With No Known Parent Package (Python). Try it out and execute the program error-free. For more informative and interesting articles, check out our website at Deasilex.

Frequently Asked Questions

Q1. What Is A Parent Package In Python?

Ans. The list of packages that Python will load is stored in this environment variable. The parent directory path will be added to the sys. path because the list of packages contained in PYTHONPATH is also included there.

Q2. How Do I Fix Missing Imports In Python?

Ans. If you’re using Visual Studio, please navigate to the options, look for the Advance path, and then add it (/. source); this should fix the issue.

Q3. How Do I Reinstall A Python Module?

Ans. Modules and packages can be installed using Python package management (pip). To install a module across the entire system, open a terminal and type the pip command.

Q4. How Do You Pass A Relative Path In Python?

Ans. An absolute path begins with /,./, or../. In Python, you must first locate the working directory where the script or module is kept in order to obtain a relative path. You can then obtain the relative path to the desired file from that place.

Q5. What Is __ init __ py For?

Ans. Python must use the __init .py files to consider folders containing the file as packages. This stops common folders, like string, from accidentally hiding valid modules that appear later on the module search path.

Leave a Comment

Your email address will not be published. Required fields are marked *