âčïž Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.2 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| URL | https://realpython.com/absolute-vs-relative-python-imports/ | |||||||||
| Last Crawled | 2026-04-16 14:12:22 (6 days ago) | |||||||||
| First Indexed | 2018-09-19 14:56:36 (7 years ago) | |||||||||
| HTTP Status Code | 200 | |||||||||
| Content | ||||||||||
| Meta Title | Absolute vs Relative Imports in Python â Real Python | |||||||||
| Meta Description | 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. In this tutorial, youâll not only cover the pros and cons of absolute and relative imports but also learn about the best practices for writing import statements. | |||||||||
| Meta Canonical | null | |||||||||
| Boilerpipe Text | 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.
Even for Pythonistas with a couple of projects under their belt, imports can be confusing! Youâre probably reading this because youâd like to gain a deeper understanding of imports in Python, particularly absolute and relative imports.
In this tutorial, youâll learn the differences between the two, as well as their pros and cons. Letâs dive right in!
A Quick Recap on Imports
You need to have a good understanding of
Python modules and packages
to know how imports work. A Python module is a file that has a
.py
extension, and a Python package is any folder that has modules inside it (or, in Python 2, a folder that contains an
__init__.py
file).
What happens when you have code in one module that needs to access code in another module or package? You import it!
How Imports Work
But how exactly do imports work? Letâs say you import a module
abc
like so:
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
!
Syntax of Import Statements
Now that you know how import statements work, letâs explore their syntax. You can import both packages and modules. (Note that importing a package essentially imports the packageâs
__init__.py
file as a module.) You can also import specific objects from a package or module.
There are generally two types of import syntax. When you use the first one, you import the resource directly, like this:
abc
can be a package or a module.
When you use the second syntax, you import the resource from another package or module. Hereâs an example:
xyz
can be a module, subpackage, or object, such as a class or function.
You can also choose to rename an imported resource, like so:
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.
Styling of Import Statements
PEP 8
, the official
style guide for Python
, has a few pointers when it comes to writing import statements. Hereâs a summary:
Imports should always be written at the top of the file, after any module comments and
docstrings
.
Imports should be divided according to what is being imported. There are generally three groups:
standard library imports (Pythonâs built-in modules)
related third party imports (modules that are installed and do not belong to the current application)
local application imports (modules that belong to the current application)
Each group of imports should be separated by a blank line.
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.
Hereâs an example of how to style import statements:
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.
Syntax and Practical Examples
Letâs say you have the following directory structure:
Thereâs a directory,
project
, which contains two sub-directories,
package1
and
package2
. The
package1
directory has two files,
module1.py
and
module2.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
.
Letâs assume the following:
package1/module2.py
contains a function,
function1
.
package2/__init__.py
contains a class,
class1
.
package2/subpackage1/module5.py
contains a function,
function2
.
The following are practical examples of absolute imports:
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 (
/
).
Pros and Cons of Absolute Imports
Absolute imports are preferred because they are quite clear and straightforward. It is easy to tell exactly where the imported resource is, just by looking at the statement. Additionally, absolute imports remain valid even if the current location of the import statement changes. In fact, PEP 8 explicitly recommends absolute imports.
Sometimes, however, absolute imports can get quite verbose, depending on the complexity of the directory structure. Imagine having a statement like this:
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.
Syntax and Practical Examples
The syntax of a relative import depends on the current location as well as the location of the module, package, or object to be imported. Here are a few examples of relative imports:
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!
Letâs assume you have the same directory structure as before:
Recall the file contents:
package1/module2.py
contains a function,
function1
.
package2/__init__.py
contains a class,
class1
.
package2/subpackage1/module5.py
contains a function,
function2
.
You can import
function1
into the
package1/module1.py
file this way:
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:
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
.
Pros and Cons of Relative Imports
One clear advantage of relative imports is that they are quite succinct. Depending on the current location, they can turn the ridiculously long import statement you saw earlier to something as simple as this:
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.
Conclusion
Good job for making it to the end of this crash course on absolute and relative imports! Now youâre up to speed on how imports work. Youâve learned the best practices for writing import statements, and you know the difference between absolute and relative imports.
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.
Thanks for reading! | |||||||||
| Markdown | [](https://realpython.com/)
- [Start Here](https://realpython.com/start-here/)
- [Learn Python](https://realpython.com/absolute-vs-relative-python-imports/)
[Python Tutorials â In-depth articles and video courses](https://realpython.com/search?kind=article&kind=course&order=newest)
[Learning Paths â Guided study plans for accelerated learning](https://realpython.com/learning-paths/)
[Quizzes & Exercises â Check your learning progress](https://realpython.com/quizzes/)
[Browse Topics â Focus on a specific area or skill level](https://realpython.com/tutorials/all/)
[Community Chat â Learn with other Pythonistas](https://realpython.com/community/)
[Office Hours â Live Q\&A calls with Python experts](https://realpython.com/office-hours/)
[Live Courses â Live, instructor-led Python courses](https://realpython.com/live/)
[Podcast â Hear whatâs new in the world of Python](https://realpython.com/podcasts/rpp/)
[Books â Round out your knowledge and learn offline](https://realpython.com/products/books/)
[Reference â Concise definitions for common Python terms](https://realpython.com/ref/)
[Code Mentor âBeta Personalized code assistance & learning tools](https://realpython.com/mentor/)
[Unlock All Content â](https://realpython.com/account/join/)
- [More](https://realpython.com/absolute-vs-relative-python-imports/)
[Learner Stories](https://realpython.com/learner-stories/) [Python Newsletter](https://realpython.com/newsletter/) [Python Job Board](https://www.pythonjobshq.com/) [Meet the Team](https://realpython.com/team/) [Become a Contributor](https://realpython.com/jobs/)
- [Search](https://realpython.com/search "Search")
- [Join](https://realpython.com/account/join/)
- [SignâIn](https://realpython.com/account/login/?next=%2Fabsolute-vs-relative-python-imports%2F)
[Browse Topics](https://realpython.com/tutorials/all/)
[Guided Learning Paths](https://realpython.com/learning-paths/)
[Basics](https://realpython.com/search?level=basics)
[Intermediate](https://realpython.com/search?level=intermediate)
[Advanced](https://realpython.com/search?level=advanced)
***
[ai](https://realpython.com/tutorials/ai/) [algorithms](https://realpython.com/tutorials/algorithms/) [api](https://realpython.com/tutorials/api/) [best-practices](https://realpython.com/tutorials/best-practices/) [career](https://realpython.com/tutorials/career/) [community](https://realpython.com/tutorials/community/) [databases](https://realpython.com/tutorials/databases/) [data-science](https://realpython.com/tutorials/data-science/) [data-structures](https://realpython.com/tutorials/data-structures/) [data-viz](https://realpython.com/tutorials/data-viz/) [devops](https://realpython.com/tutorials/devops/) [django](https://realpython.com/tutorials/django/) [docker](https://realpython.com/tutorials/docker/) [editors](https://realpython.com/tutorials/editors/) [flask](https://realpython.com/tutorials/flask/) [front-end](https://realpython.com/tutorials/front-end/) [gamedev](https://realpython.com/tutorials/gamedev/) [gui](https://realpython.com/tutorials/gui/) [machine-learning](https://realpython.com/tutorials/machine-learning/) [news](https://realpython.com/tutorials/news/) [numpy](https://realpython.com/tutorials/numpy/) [projects](https://realpython.com/tutorials/projects/) [python](https://realpython.com/tutorials/python/) [stdlib](https://realpython.com/tutorials/stdlib/) [testing](https://realpython.com/tutorials/testing/) [tools](https://realpython.com/tutorials/tools/) [web-dev](https://realpython.com/tutorials/web-dev/) [web-scraping](https://realpython.com/tutorials/web-scraping/)
[Table of Contents](https://realpython.com/absolute-vs-relative-python-imports/#toc)
- [A Quick Recap on Imports](https://realpython.com/absolute-vs-relative-python-imports/#a-quick-recap-on-imports)
- [How Imports Work](https://realpython.com/absolute-vs-relative-python-imports/#how-imports-work)
- [Syntax of Import Statements](https://realpython.com/absolute-vs-relative-python-imports/#syntax-of-import-statements)
- [Styling of Import Statements](https://realpython.com/absolute-vs-relative-python-imports/#styling-of-import-statements)
- [Absolute Imports](https://realpython.com/absolute-vs-relative-python-imports/#absolute-imports)
- [Syntax and Practical Examples](https://realpython.com/absolute-vs-relative-python-imports/#syntax-and-practical-examples)
- [Pros and Cons of Absolute Imports](https://realpython.com/absolute-vs-relative-python-imports/#pros-and-cons-of-absolute-imports)
- [Relative Imports](https://realpython.com/absolute-vs-relative-python-imports/#relative-imports)
- [Syntax and Practical Examples](https://realpython.com/absolute-vs-relative-python-imports/#syntax-and-practical-examples_1)
- [Pros and Cons of Relative Imports](https://realpython.com/absolute-vs-relative-python-imports/#pros-and-cons-of-relative-imports)
- [Conclusion](https://realpython.com/absolute-vs-relative-python-imports/#conclusion)
Mark as Completed
Share
Recommended Course
[ Absolute vs Relative Imports in Python 16m · 4 lessons](https://realpython.com/courses/absolute-vs-relative-imports-python/)

# Absolute vs Relative Imports in Python
by [Mbithe Nzomo](https://realpython.com/absolute-vs-relative-python-imports/#author)
Reading time estimate
9m
[33 Comments](https://realpython.com/absolute-vs-relative-python-imports/#reader-comments)
[intermediate](https://realpython.com/tutorials/intermediate/) [best-practices](https://realpython.com/tutorials/best-practices/) [python](https://realpython.com/tutorials/python/)
Mark as Completed
Share
Table of Contents
- [A Quick Recap on Imports](https://realpython.com/absolute-vs-relative-python-imports/#a-quick-recap-on-imports)
- [How Imports Work](https://realpython.com/absolute-vs-relative-python-imports/#how-imports-work)
- [Syntax of Import Statements](https://realpython.com/absolute-vs-relative-python-imports/#syntax-of-import-statements)
- [Styling of Import Statements](https://realpython.com/absolute-vs-relative-python-imports/#styling-of-import-statements)
- [Absolute Imports](https://realpython.com/absolute-vs-relative-python-imports/#absolute-imports)
- [Syntax and Practical Examples](https://realpython.com/absolute-vs-relative-python-imports/#syntax-and-practical-examples)
- [Pros and Cons of Absolute Imports](https://realpython.com/absolute-vs-relative-python-imports/#pros-and-cons-of-absolute-imports)
- [Relative Imports](https://realpython.com/absolute-vs-relative-python-imports/#relative-imports)
- [Syntax and Practical Examples](https://realpython.com/absolute-vs-relative-python-imports/#syntax-and-practical-examples_1)
- [Pros and Cons of Relative Imports](https://realpython.com/absolute-vs-relative-python-imports/#pros-and-cons-of-relative-imports)
- [Conclusion](https://realpython.com/absolute-vs-relative-python-imports/#conclusion)
[Remove ads](https://realpython.com/account/join/)
Recommended Course
[Absolute vs Relative Imports in Python](https://realpython.com/courses/absolute-vs-relative-imports-python/) (16m)
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.
Even for Pythonistas with a couple of projects under their belt, imports can be confusing! Youâre probably reading this because youâd like to gain a deeper understanding of imports in Python, particularly absolute and relative imports.
In this tutorial, youâll learn the differences between the two, as well as their pros and cons. Letâs dive right in\!
**Free Bonus:** [5 Thoughts On Python Mastery](https://realpython.com/bonus/python-mastery-course/), a free course for Python developers that shows you the roadmap and the mindset youâll need to take your Python skills to the next level.
## A Quick Recap on Imports
You need to have a good understanding of [Python modules and packages](https://realpython.com/python-modules-packages/) to know how imports work. A Python module is a file that has a `.py` extension, and a Python package is any folder that has modules inside it (or, in Python 2, a folder that contains an `__init__.py` file).
What happens when you have code in one module that needs to access code in another module or package? You import it\!
[Remove ads](https://realpython.com/account/join/)
### How Imports Work
But how exactly do imports work? Letâs say you import a module `abc` like so:
Python
```
import abc
```
The first thing Python will do is look up the name `abc` in [`sys.modules`](https://docs.python.org/3/library/sys.html#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](https://docs.python.org/3/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`](https://docs.python.org/3/library/sys.html#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](https://docs.python.org/3/reference/import.html)\!
**Note: Security Concerns**
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:
- [10 common security gotchas in Python and how to avoid them](https://hackernoon.com/10-common-security-gotchas-in-python-and-how-to-avoid-them-e19fbe265e03) by Anthony Shaw (Point 5 talks about Pythonâs import system.)
- [Episode \#168: 10 Python security holes and how to plug them](https://talkpython.fm/episodes/show/168/10-python-security-holes-and-how-to-plug-them) from the TalkPython podcast (The panelists begin talking about imports at around the 27:15 mark.)
### Syntax of Import Statements
Now that you know how import statements work, letâs explore their syntax. You can import both packages and modules. (Note that importing a package essentially imports the packageâs `__init__.py` file as a module.) You can also import specific objects from a package or module.
There are generally two types of import syntax. When you use the first one, you import the resource directly, like this:
Python
```
import abc
```
`abc` can be a package or a module.
When you use the second syntax, you import the resource from another package or module. Hereâs an example:
Python
```
from abc import xyz
```
`xyz` can be a module, subpackage, or object, such as a class or function.
You can also choose to rename an imported resource, like so:
Python
```
import abc as other_name
```
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.
### Styling of Import Statements
[PEP 8](http://pep8.org/#imports), the official [style guide for Python](https://realpython.com/python-code-quality/), has a few pointers when it comes to writing import statements. Hereâs a summary:
1. Imports should always be written at the top of the file, after any module comments and [docstrings](https://realpython.com/how-to-write-docstrings-in-python/).
2. Imports should be divided according to what is being imported. There are generally three groups:
- standard library imports (Pythonâs built-in modules)
- related third party imports (modules that are installed and do not belong to the current application)
- local application imports (modules that belong to the current application)
3. Each group of imports should be separated by a blank line.
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.
Hereâs an example of how to style import statements:
Python
```
```
The import statements above are divided into three distinct groups, separated by a blank space. They are also ordered alphabetically within each group.
[Remove ads](https://realpython.com/account/join/)
## 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.
### Syntax and Practical Examples
Letâs say you have the following directory structure:
Shell
```
```
Thereâs a directory, `project`, which contains two sub-directories, `package1` and `package2`. The `package1` directory has two files, `module1.py` and `module2.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`.
Letâs assume the following:
1. `package1/module2.py` contains a function, `function1`.
2. `package2/__init__.py` contains a class, `class1`.
3. `package2/subpackage1/module5.py` contains a function, `function2`.
The following are practical examples of absolute imports:
Python
```
```
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 (`/`).
### Pros and Cons of Absolute Imports
Absolute imports are preferred because they are quite clear and straightforward. It is easy to tell exactly where the imported resource is, just by looking at the statement. Additionally, absolute imports remain valid even if the current location of the import statement changes. In fact, PEP 8 explicitly recommends absolute imports.
Sometimes, however, absolute imports can get quite verbose, depending on the complexity of the directory structure. Imagine having a statement like this:
Python
```
from package1.subpackage2.subpackage3.subpackage4.module5 import function6
```
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.
[Remove ads](https://realpython.com/account/join/)
### Syntax and Practical Examples
The syntax of a relative import depends on the current location as well as the location of the module, package, or object to be imported. Here are a few examples of relative imports:
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\!
Letâs assume you have the same directory structure as before:
Shell
```
```
Recall the file contents:
1. `package1/module2.py` contains a function, `function1`.
2. `package2/__init__.py` contains a class, `class1`.
3. `package2/subpackage1/module5.py` contains a function, `function2`.
You can import `function1` into the `package1/module1.py` file this way:
Python
```
```
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
```
```
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`.
### Pros and Cons of Relative Imports
One clear advantage of relative imports is that they are quite succinct. Depending on the current location, they can turn the ridiculously long import statement you saw earlier to something as simple as this:
Python
```
from ..subpackage4.module5 import function6
```
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.
[Remove ads](https://realpython.com/account/join/)
## Conclusion
Good job for making it to the end of this crash course on absolute and relative imports! Now youâre up to speed on how imports work. Youâve learned the best practices for writing import statements, and you know the difference between absolute and relative imports.
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.
Thanks for reading\!
Mark as Completed
Share
Recommended Course
[Absolute vs Relative Imports in Python](https://realpython.com/courses/absolute-vs-relative-imports-python/) (16m)
đ 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.

About **Mbithe Nzomo**
[ ](https://realpython.com/team/mnzomo/)
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.
[» More about Mbithe](https://realpython.com/team/mnzomo/)
***
*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:*
[](https://realpython.com/team/acutenco/)
[Adriana](https://realpython.com/team/acutenco/)
[](https://realpython.com/team/damos/)
[David](https://realpython.com/team/damos/)
[](https://realpython.com/team/jjablonski/)
[Joanna](https://realpython.com/team/jjablonski/)
Master Real-World Python Skills With Unlimited Access to Real Python

**Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:**
[Level Up Your Python Skills »](https://realpython.com/account/join/?utm_source=rp_article_footer&utm_content=absolute-vs-relative-python-imports)
Master Real-World Python Skills
With Unlimited Access to Real Python

**Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:**
[Level Up Your Python Skills »](https://realpython.com/account/join/?utm_source=rp_article_footer&utm_content=absolute-vs-relative-python-imports)
What Do You Think?
**Rate this article:**
[LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Frealpython.com%2Fabsolute-vs-relative-python-imports%2F)
[Twitter](https://twitter.com/intent/tweet/?text=Interesting%20Python%20article%20on%20%40realpython%3A%20Absolute%20vs%20Relative%20Imports%20in%20Python&url=https%3A%2F%2Frealpython.com%2Fabsolute-vs-relative-python-imports%2F)
[Bluesky](https://bsky.app/intent/compose?text=Interesting%20Python%20article%20on%20%40realpython.com%3A%20Absolute%20vs%20Relative%20Imports%20in%20Python%20https%3A%2F%2Frealpython.com%2Fabsolute-vs-relative-python-imports%2F)
[Facebook](https://facebook.com/sharer/sharer.php?u=https%3A%2F%2Frealpython.com%2Fabsolute-vs-relative-python-imports%2F)
[Email](mailto:?subject=Python%20article%20for%20you&body=Absolute%20vs%20Relative%20Imports%20in%20Python%20on%20Real%20Python%0A%0Ahttps%3A%2F%2Frealpython.com%2Fabsolute-vs-relative-python-imports%2F%0A)
Whatâs your \#1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.
**Commenting Tips:** The most useful comments are those written with the goal of learning from or helping out other students. [Get tips for asking good questions](https://realpython.com/python-beginner-tips/#tip-9-ask-good-questions) and [get answers to common questions in our support portal](https://support.realpython.com/).
***
Looking for a real-time conversation? Visit the [Real Python Community Chat](https://realpython.com/community/) or join the next [âOffice Hoursâ Live Q\&A Session](https://realpython.com/office-hours/). Happy Pythoning\!
Keep Learning
Related Topics: [intermediate](https://realpython.com/tutorials/intermediate/) [best-practices](https://realpython.com/tutorials/best-practices/) [python](https://realpython.com/tutorials/python/)
Related Learning Paths:
- [Modules and Packages](https://realpython.com/learning-paths/modules-and-packages/?utm_source=realpython&utm_medium=web&utm_campaign=related-learning-path&utm_content=absolute-vs-relative-python-imports)
Related Courses:
- [Absolute vs Relative Imports in Python](https://realpython.com/courses/absolute-vs-relative-imports-python/?utm_source=realpython&utm_medium=web&utm_campaign=related-course&utm_content=absolute-vs-relative-python-imports)
Related Tutorials:
- [Python import: Advanced Techniques and Tips](https://realpython.com/python-import/?utm_source=realpython&utm_medium=web&utm_campaign=related-post&utm_content=absolute-vs-relative-python-imports)
- [Python Modules and Packages â An Introduction](https://realpython.com/python-modules-packages/?utm_source=realpython&utm_medium=web&utm_campaign=related-post&utm_content=absolute-vs-relative-python-imports)
- [What's a Python Namespace Package, and What's It For?](https://realpython.com/python-namespace-package/?utm_source=realpython&utm_medium=web&utm_campaign=related-post&utm_content=absolute-vs-relative-python-imports)
- [What Is Python's \_\_init\_\_.py For?](https://realpython.com/python-init-py/?utm_source=realpython&utm_medium=web&utm_campaign=related-post&utm_content=absolute-vs-relative-python-imports)
- [Python Type Checking (Guide)](https://realpython.com/python-type-checking/?utm_source=realpython&utm_medium=web&utm_campaign=related-post&utm_content=absolute-vs-relative-python-imports)
## Keep reading Real Python by creating a free account or signing in:
[](https://realpython.com/account/signup/?intent=continue_reading&utm_source=rp&utm_medium=web&utm_campaign=rwn&utm_content=v1&next=%2Fabsolute-vs-relative-python-imports%2F)
[Continue »](https://realpython.com/account/signup/?intent=continue_reading&utm_source=rp&utm_medium=web&utm_campaign=rwn&utm_content=v1&next=%2Fabsolute-vs-relative-python-imports%2F)
Already have an account? [Sign-In](https://realpython.com/account/login/?next=/absolute-vs-relative-python-imports/)
Almost there! Complete this form and click the button below to gain instant access:
Ă

5 Thoughts On Python Mastery
##### Learn Python
- [Start Here](https://realpython.com/start-here/)
- [Learning Resources](https://realpython.com/search)
- [Code Mentor](https://realpython.com/mentor/)
- [Python Reference](https://realpython.com/ref/)
- [Python Cheat Sheet](https://realpython.com/cheatsheets/python/)
- [Support Center](https://support.realpython.com/)
##### Courses & Paths
- [Learning Paths](https://realpython.com/learning-paths/)
- [Quizzes & Exercises](https://realpython.com/quizzes/)
- [Browse Topics](https://realpython.com/tutorials/all/)
- [Live Courses](https://realpython.com/live/)
- [Books](https://realpython.com/books/)
##### Community
- [Podcast](https://realpython.com/podcasts/rpp/)
- [Newsletter](https://realpython.com/newsletter/)
- [Community Chat](https://realpython.com/community/)
- [Office Hours](https://realpython.com/office-hours/)
- [Learner Stories](https://realpython.com/learner-stories/)
##### Membership
- [Plans & Pricing](https://realpython.com/account/join/)
- [Team Plans](https://realpython.com/account/join-team/)
- [For Business](https://realpython.com/account/join-team/inquiry/)
- [For Schools](https://realpython.com/account/join-team/education-inquiry/)
- [Reviews](https://realpython.com/learner-stories/)
##### Company
- [About Us](https://realpython.com/about/)
- [Team](https://realpython.com/team/)
- [Mission & Values](https://realpython.com/mission/)
- [Editorial Guidelines](https://realpython.com/editorial-guidelines/)
- [Sponsorships](https://realpython.com/sponsorships/)
- [Careers](https://realpython.workable.com/)
- [Press Kit](https://realpython.com/media-kit/)
- [Merch](https://realpython.com/merch)
[Privacy Policy](https://realpython.com/privacy-policy/) â
[Terms of Use](https://realpython.com/terms/) â
[Security](https://realpython.com/security/) â
[Contact](https://realpython.com/contact/)
Happy Pythoning\!
© 2012â2026 DevCademy Media Inc. DBA Real Python. All rights reserved.
REALPYTHONâą is a trademark of DevCademy Media Inc.
[](https://realpython.com/)

You've blocked notifications | |||||||||
| Readable Markdown | 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.
Even for Pythonistas with a couple of projects under their belt, imports can be confusing! Youâre probably reading this because youâd like to gain a deeper understanding of imports in Python, particularly absolute and relative imports.
In this tutorial, youâll learn the differences between the two, as well as their pros and cons. Letâs dive right in\!
## A Quick Recap on Imports
You need to have a good understanding of [Python modules and packages](https://realpython.com/python-modules-packages/) to know how imports work. A Python module is a file that has a `.py` extension, and a Python package is any folder that has modules inside it (or, in Python 2, a folder that contains an `__init__.py` file).
What happens when you have code in one module that needs to access code in another module or package? You import it\!
### How Imports Work
But how exactly do imports work? Letâs say you import a module `abc` like so:
The first thing Python will do is look up the name `abc` in [`sys.modules`](https://docs.python.org/3/library/sys.html#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](https://docs.python.org/3/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`](https://docs.python.org/3/library/sys.html#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](https://docs.python.org/3/reference/import.html)\!
### Syntax of Import Statements
Now that you know how import statements work, letâs explore their syntax. You can import both packages and modules. (Note that importing a package essentially imports the packageâs `__init__.py` file as a module.) You can also import specific objects from a package or module.
There are generally two types of import syntax. When you use the first one, you import the resource directly, like this:
`abc` can be a package or a module.
When you use the second syntax, you import the resource from another package or module. Hereâs an example:
`xyz` can be a module, subpackage, or object, such as a class or function.
You can also choose to rename an imported resource, like so:
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.
### Styling of Import Statements
[PEP 8](http://pep8.org/#imports), the official [style guide for Python](https://realpython.com/python-code-quality/), has a few pointers when it comes to writing import statements. Hereâs a summary:
1. Imports should always be written at the top of the file, after any module comments and [docstrings](https://realpython.com/how-to-write-docstrings-in-python/).
2. Imports should be divided according to what is being imported. There are generally three groups:
- standard library imports (Pythonâs built-in modules)
- related third party imports (modules that are installed and do not belong to the current application)
- local application imports (modules that belong to the current application)
3. Each group of imports should be separated by a blank line.
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.
Hereâs an example of how to style import statements:
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.
### Syntax and Practical Examples
Letâs say you have the following directory structure:
Thereâs a directory, `project`, which contains two sub-directories, `package1` and `package2`. The `package1` directory has two files, `module1.py` and `module2.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`.
Letâs assume the following:
1. `package1/module2.py` contains a function, `function1`.
2. `package2/__init__.py` contains a class, `class1`.
3. `package2/subpackage1/module5.py` contains a function, `function2`.
The following are practical examples of absolute imports:
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 (`/`).
### Pros and Cons of Absolute Imports
Absolute imports are preferred because they are quite clear and straightforward. It is easy to tell exactly where the imported resource is, just by looking at the statement. Additionally, absolute imports remain valid even if the current location of the import statement changes. In fact, PEP 8 explicitly recommends absolute imports.
Sometimes, however, absolute imports can get quite verbose, depending on the complexity of the directory structure. Imagine having a statement like this:
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.
### Syntax and Practical Examples
The syntax of a relative import depends on the current location as well as the location of the module, package, or object to be imported. Here are a few examples of relative imports:
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\!
Letâs assume you have the same directory structure as before:
Recall the file contents:
1. `package1/module2.py` contains a function, `function1`.
2. `package2/__init__.py` contains a class, `class1`.
3. `package2/subpackage1/module5.py` contains a function, `function2`.
You can import `function1` into the `package1/module1.py` file this way:
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:
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`.
### Pros and Cons of Relative Imports
One clear advantage of relative imports is that they are quite succinct. Depending on the current location, they can turn the ridiculously long import statement you saw earlier to something as simple as this:
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.
## Conclusion
Good job for making it to the end of this crash course on absolute and relative imports! Now youâre up to speed on how imports work. Youâve learned the best practices for writing import statements, and you know the difference between absolute and relative imports.
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.
Thanks for reading\! | |||||||||
| ML Classification | ||||||||||
| ML Categories |
Raw JSON{
"/Computers_and_Electronics": 988,
"/Computers_and_Electronics/Programming": 956,
"/Computers_and_Electronics/Programming/Scripting_Languages": 922
} | |||||||||
| ML Page Types |
Raw JSON{
"/Article": 999,
"/Article/Tutorial_or_Guide": 993
} | |||||||||
| ML Intent Types |
Raw JSON{
"Informational": 999
} | |||||||||
| Content Metadata | ||||||||||
| Language | en | |||||||||
| Author | Real Python | |||||||||
| Publish Time | not set | |||||||||
| Original Publish Time | 2018-09-19 14:56:36 (7 years ago) | |||||||||
| Republished | No | |||||||||
| Word Count (Total) | 2,566 | |||||||||
| Word Count (Content) | 1,412 | |||||||||
| Links | ||||||||||
| External Links | 23 | |||||||||
| Internal Links | 95 | |||||||||
| Technical SEO | ||||||||||
| Meta Nofollow | No | |||||||||
| Meta Noarchive | No | |||||||||
| JS Rendered | Yes | |||||||||
| Redirect Target | null | |||||||||
| Performance | ||||||||||
| Download Time (ms) | 214 | |||||||||
| TTFB (ms) | 190 | |||||||||
| Download Size (bytes) | 20,807 | |||||||||
| Shard | 71 (laksa) | |||||||||
| Root Hash | 13351397557425671 | |||||||||
| Unparsed URL | com,realpython!/absolute-vs-relative-python-imports/ s443 | |||||||||