Especially if you are a beginner Python programmer, module imports can be extremely frustrating. Imports are explained in detail in this tutorial, along with how to resolve the error message ImportError: attempted relative import with no known parent package.
We should first understand a few basic Python terminologies before proceeding with the solution.
Module for Python: Modules are files that contain definitions and statements in the Python programming language.
It is possible for a module to contain both executable statements and definitions of function calls. Think of a single.py file that contains some functionality as an example.
One or more modules comprise a Python package, which is identified by the file init .py, which informs Python that the directory in which it is located is part of a package. There may be nothing in the init file, or it may contain scripting code that will be executed when the package is first loaded.
When writing Python code, imports are essential for structuring your code effectively. By using the import keyword, you can import any module and reuse it in a productive manner.
Relative import and absolute import are the two types of import that will be discussed in greater detail.
Consider the following example of a project folder.
└── yourProject ├── firstPackage │ ├── packageFile1.py └── secondPackage ├── packageFile2.py ├── packageFile3.py └── subPackage └── packageFile4.py
The project described above is divided into two packages, firstPackage, and secondPackage. Each of these packages contains a number of modules, and the second package also contains a subPackage that contains a module of its own. This is typically how a project structure is laid out, and it can become quite complicated.
How Does Module Import Work In Python?
Here is an example of an import statement in one of your files, as shown below.
Python will carry out the following operations in order to import the module:
- The requested module should be located and loaded, as well as initialized (if necessary).
- Set up your local namespace and corresponding scopes with the names that you’ll need later.
The following steps will be taken by the Python interpreter in an attempt to resolve the module packageFile2.
1. Lookup sys.modules
First, Python will attempt to locate the module in the system.modules dictionary, which contains a mapping of key-value pairs corresponding to module names. If it locates the module, it is resolved and loaded into the system.
2. Python Standard Library lookup
Among the many features of the Python Standard Library are built-in modules (written in C), which provide access to system functionality such as file I/O, which would otherwise be unavailable to Python programmers.
Python modules are written to provide standardized solutions for a wide range of problems that arise in the course of everyday programming. By abstracting away platform-specifics into platform-neutral APIs and making them available to all Python programs, some of these modules are explicitly designed to encourage and enhance the portability of Python programs.
Whenever a name is not found in the system modules, the search is expanded to include the standard library. If it is unable to locate something over there, it proceeds to the next step.
3. Lookup sys.path
As the final step in resolving the module, Python will look into the sys.path variable. ModuleNotFoundError: This is where things can go wrong, and you will receive the following message. There is no module named ‘packageFile2’.
How to fix ImportError: attempted relative import with no known parent package
To fix the importError: attempted relative import with no know parent package error you can follow the below steps.
1. Using Absolute Imports
Let us take the below example of your current project structure.
├── project │ ├── package │ │ ├── __init__.py │ │ ├── module.py │ │ └── standalone.py │ └── setup.py
And make sure if you want to import setup then your setup.py should contain the following code in it.
from setuptools import setup, find_packages setup( name = 'package_you_want_to_import', packages = find_packages(), )
2. Do Not Use From Keyword
Remove the from keyword and import using the standard method as shown in the example below.
3. Making Use Of __init__.py
Suppose you have a project structure that is as follow then you can make use of __init__.py to get rid of this error.
├── project │ ├── package │ │ ├── __init__.py │ │ ├── firstModule.py │ │ └── secondModule.py │ └── setup.py
Then use the regular imports as below.
# in secondModule.py from firstModule import firstClass
I hope you were able to resolve the above import error using the methods described above. I’ve listed three approaches you can take to fix this problem in your project.
Please let me know in the comments section if you are still having problems or if you have a better solution than the one discussed above, and I will be happy to assist you.