Python - Package Relative Imports

Introduction

For example:

from . import test                        # Relative to this package 

instructs Python to import a module named test located in the same package directory as the file in which this statement appears.

Similarly, this statement:

from .test import name 

means "from a module named test located in the same package, import the variable name."

Other form

Other dot-based relative reference patterns are possible.

Within a module file located in a package directory named mypkg, the following alternative import forms work as described:

from .string import name1, name2          # Imports names from mypkg.string 
from . import string                      # Imports mypkg.string 
from .. import string                     # Imports string sibling of mypkg 

Related Topic