Absolute Vs Relative Imports in Python
Absolute Vs Relative Imports in Python
Python Tricks
Email…
Python time.
• Absolute Imports
◦ Syntax and Practical Examples
◦ Pros and Cons of Absolute Imports
• Relative Imports
◦ Syntax and Practical Examples
◦ Pros and Cons of Relative Imports
• Conclusion
If you’ve worked on a Python project that has more than one file, chances are you’ve
had to use an import statement before.
Table of Contents
Even for Pythonistas with a couple of projects under their belt, imports can be
• A Quick Recap on Imports
confusing! You’re probably reading this because you’d like to gain a deeper
• Absolute Imports
understanding of imports in Python, particularly absolute and relative imports.
• Relative Imports
In this tutorial, you’ll learn the differences between the two, as well as their pros and • Conclusion
cons. Let’s dive right in!
What happens when you have code in one module that needs to access code in
another module or package? You import it!
Python
import abc
The first thing Python will do is look up the name abc in sys.modules. This is a cache
of all modules that have been previously imported.
If the name isn’t found in the module cache, Python will proceed to search through a
list of built-in modules. These are modules that come pre-installed with Python and
can be found in the Python Standard Library. If the name still isn’t found in the built-
in modules, Python then searches for it in a list of directories defined by sys.path.
This list usually includes the current directory, which is searched first.
When Python finds the module, it binds it to a name in the local scope. This means
that abc is now defined and can be used in the current file without throwing a
NameError.
If the name is never found, you’ll get a ModuleNotFoundError. You can find out more
about imports in the Python documentation here!
Be aware that Python’s import system presents some significant security risks.
This is largely due to its flexibility. For example, the module cache is writable,
and it is possible to override core Python functionality using the import
system. Importing from third-party packages can also expose your application
to security threats.
Here are a couple of interesting resources to learn more about these security
concerns and how to mitigate them:
Improve
There are generally two types of import syntax. When you use the Youryou
first one, Python
Python
import abc
When you use the second syntax, you import the resource from another package or
module. Here’s an example:
Python
Python
This renames the imported resource abc to other_name within the script. It must now
be referenced as other_name, or it will not be recognized.
1. Imports should always be written at the top of the file, after any module
comments and docstrings.
It’s also a good idea to order your imports alphabetically within each import group.
This makes finding particular imports much easier, especially when there are many
imports in a file.
Python
"""
The import statements above are divided into three distinct groups, separated by a
blank space. They are also ordered alphabetically within each group.
Absolute Imports
You’ve gotten up to speed on how to write import statements and how to style them
like a pro. Now it’s time to learn a little more about absolute imports.
An absolute import specifies the resource to be imported using its full path from the
project’s root folder.
└── project
├── package1
│ ├── module1.py
│ └── module2.py
└── package2
├── __init__.py
├── module3.py
├── module4.py
└── subpackage1
└── module5.py
The package2 directory has three files: two modules, module3.py and module4.py, and
an initialization file, __init__.py. It also contains a directory, subpackage, which in
turn contains a file, module5.py.
Python
Improve Your Python
from package1 import module1
from package1.module2 import function1
from package2 import class1
from package2.subpackage1.module5 import function2
Note that you must give a detailed path for each package or file, from the top-level
package folder. This is somewhat similar to its file path, but we use a dot (.) instead
of a slash (/).
Sometimes, however, absolute imports can get quite verbose, depending on the
complexity of the directory structure. Imagine having a statement like this:
Python
That’s ridiculous, right? Luckily, relative imports are a good alternative in such
cases!
Relative Imports
A relative import specifies the resource to be imported relative to the current
location—that is, the location where the import statement is. There are two types of
relative imports: implicit and explicit. Implicit relative imports have been
deprecated in Python 3, so I won’t be covering them here.
Python
You can see that there is at least one dot in each import statement above. Relative
imports make use of dot notation to specify location.
A single dot means that the module or package referenced is in the same directory
as the current location. Two dots mean that it is in the parent directory of the
current location—that is, the directory above. Three dots mean that it is in the
grandparent directory, and so on. This will probably be familiar to you if you use a
Unix-like operating system!
You can import function1 into the package1/module1.py file this way:
Python
# package1/module1.py
You’d use only one dot here because module2.py is in the same directory as the
current module, which is module1.py.
You can import class1 and function2 into the package2/module3.py file this way:
Python
# package2/module3.py
In the first import statement, the single dot means that you are importing class1
from the current package. Remember that importing a package essentially imports
the package’s __init__.py file as a module.
In the second import statement, you’d use a single dot again because subpackage1 is
in the same directory as the current module, which is module3.py.
Python
Unfortunately, relative imports can be messy, particularly for shared projects where
directory structure is likely to change. Relative imports are also not as readable as
absolute ones, and it’s not easy to tell the location of the imported resources.
With your new skills, you can confidently import packages and modules from the
Python standard library, third party packages, and your own local packages.
Remember that you should generally opt for absolute imports over relative ones,
unless the path is complex and would make the statement too long.
Python Tricks
Get a short & sweet Python Trick delivered to your inbox every couple of
days. No spam ever. Unsubscribe any time. Curated by the Real Python
team.
Email Address
I'm an experienced software engineer with interests in artificial intelligence and machine
learning. I'm currently a postgraduate student at The University of Manchester, studying
Advanced Computer Science with a specialisation in AI.
Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who
worked on this tutorial are:
Real Python Comment Policy: The most useful comments are those
written with the goal of learning from or helping out other readers—after
reading the whole article and all the earlier comments. Complaints and
insults generally won’t make the cut here.
Name
Best practice is probably to avoid relative imports from other package two
levels above in the file-system hierachy, but inside one package importing its
different modules it is way more readable and maintainable.
2△ ▽ • Reply • Share ›