Importing modules can sometimes cause excessive failure if you are a beginner in Python. This tutorial will teach you how to import and how to resolve importError: try importing a relative without knowing the parent package.
Before getting into the solution, let’s first understand few basic terminologies in Python.
Python Module: A module is a Python file with definitions and commands. The module can contain executable commands, such as function definitions. Simply put, think of it as a .py file with some function.
Python Package: A Python package contains one or more modules and contains a file named
__init__.py, which tells Python that this folder is a package. The hot file may be empty or may contain code that must be run at the beginning of the package.
imports: Imports in Python are essential for structuring your code effectively, and you can use the import keyword to import any module and reuse it efficiently. There are two types of imports, Relative and Absolute, that deal with depth.
Let’s consider a simple example.
└── myproject ├── firstpackage │ ├── a.py └── secondpackage ├── b.py ├── c.py └── subpackage └── d.py
The above project has two packages named firstpackage and secondpackage. Each of these contains some modules, and the secondpackage also has a subpackage that includes its own module. Typically the project structure goes something like this, and it may grow pretty complex.
How does module import work in Python?
Now, let’s say if you import module b in one of your files using the import statement as shown below.
Python will perform the following operations to import the module:
- Locate, load, and initialize (if required) the requested module
- Define necessary names in the local namespace and corresponding scope
Now Python interpreter is going to follow the following steps in an attempt to resolve module b .
Python will try to look at the module first in the
sys.modules, which is a dictionary that has a mapping of key-value pairs of modules. If it finds, then the module is resolved and loaded.
Step 2: Python Standard Library lookup
Python Standard Library contains built-in modules (written in C) that provide access to system functionality such as file I/O that would otherwise be inaccessible to Python programmers. Modules are written in Python that provides standardized solutions for many problems that occur in everyday programming. Some of these modules are explicitly designed to encourage and enhance the portability of Python programs by abstracting away platform-specifics into platform-neutral APIs.
If the name is not found in the
sys.modules, it will search in the standard library. If it cannot find over there, then it goes to the next step.
Python will look into the sys.path as the last step to resolve the module. This is where things can go wrong, and you will get ModuleNotFoundError: No module named ‘b’
Absolute vs. Relative imports
In absolute imports, you need to specify the explicit path from the project’s root directory.
Example – If we have to import module b then we can use the following way to import
Other ways of importing modules in Python
# importing modules a.py import secondpackage.subpackage.d import secondpackage.c
In case of relative imports, we need to specify the module’s path relative to the current module’s location.
# in module a.py from ..secondpackage import b from ..secondpackage.b import another_function
# in module b from . import c from .c import my_function
How to fix ImportError: attempted relative import with no known parent package?
Option 1 – Use absolute imports
For instance, the directory structure may be as follows
. ├── project │ ├── package │ │ ├── __init__.py │ │ ├── module.py │ │ └── standalone.py │ └── setup.py
where setup.py is
from setuptools import setup, find_packages setup( name = 'your_package_name', packages = find_packages(), )
Option 2 – Get rid of from keyword
Remove the from keyword and use the standard way of import as shown below.
Option 3 – Import inside package init file
Put this inside your package’s __init__.py file:
# For relative imports to work in Python 3.6 import os, sys; sys.path.append(os.path.dirname(os.path.realpath(__file__)))
Assuming your package is like this:
├── project │ ├── package │ │ ├── __init__.py │ │ ├── module1.py │ │ └── module2.py │ └── setup.py
Now use regular imports in you package, like:
# in module2.py from module1 import class1