Python “from . import x” — What’s the Purpose of the Dot?

You may have seen an import statement from . import your_module with the dot after the from keyword:

from . import your_module

What Does the Dot Mean?

In Python, modules are defined in packages. If you want to import a certain module within a package that may be hierarchically structured, you need to specify the path of your module.

└── project
     ├── your_package_1
     │   ├──
     │   └──
     └── your_package_2
     │   ├──
     │   └──

The dot (.) symbol after in an import statement of the form from . import your_module is a Python syntactical element for relative imports. It means “look for the module in your current folder”. The current folder is the one where the code file resides from which you run this import statement.

The concept of absolute and relative imports was introduced in PEP 328 to differentiate from which package a module should be imported.

  • An absolute import searches the module in your top-level package or project.
  • A relative import searches the module from the folder where the current Python file executing the import statement resides.

When using relative imports you can use more than one dot to refer to the parent folders within your packages. For example, two dots in from .. import your_module would import the module from the parent folder, and three dots in from ... import your_module would import the module from the parent of the parent.

from .. import your_module      # searches "" in the parent folder
from ... import your_module     # searches "" in the grandparent folder

You can see this example in the following graphic in a sample PyCharm project:

You add two import statements to your file with relative imports. The first imports from the current sub-package. The second imports from the parent package. Even though both imported modules have the same names, they can be differentiated through the use of relative (and absolute paths).

By the way, if you want to boost your PyCharm skills—one of the highly-leveraged activities you can do as a programmer—check out our Finxter Academy course that offers you a full-fledged mastery introduction into the ins and outs of PyCharm.

*** Join Course: Mastering the PyCharm IDE for Maximum Python Productivity ***