Relative Imports examples in Python 3.x

In Python Programming, we can make use of relative imports to import packages or modules that are located either in the same package or subpackage of the current module.

Let's take a look at a few examples.


Example 1: Import a module from the same package:

from . import my_module

Example 2: Import from a sibling package:

from ..my_package2 import my_package2 

Example 3: Importing a module from a subpackage:

from .subpackage2 import submodule2

Example 4: Import a module from a parent package:

from ..parent_module import parent_function

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!