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

Facing issues? Have Questions? Post them here! I am happy to answer!

Author: Rakesh
Author Info:

Rakesh is a seasoned developer with over 10 years of experience in web and app development, and a deep knowledge of operating systems. Author of insightful How-To articles for Code2care.

Follow him on: X

Copyright © Code2care 2023 | Privacy Policy | About Us | Contact Us | Sitemap