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 Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail:

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