Relative imports in Python 3

Relative imports in Python 3 allow you to import modules or functions from other packages within your package hierarchy. This is useful when you want to import a module or function that is located in a different package but still within your project.

To use relative imports, you need to use the from . import syntax. The number of dots before the import statement represents the number of package levels you need to go up to reach the desired module or function. For example, if you want to import a module module2 located in a package package2 that is located within your current package package1, you would use the following syntax:

from .package2 import module2

Watch a course Python - The Practical Guide

If you want to import a function function2 located in a package package2 that is located within your current package package1, you would use the following syntax:

from .package2.module2 import function2

It is important to note that relative imports are only supported within the same package hierarchy and are not recommended for use in external libraries or modules.