Absolute vs Relative Imports in Python: Overview
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!
If you’re reading this because you’d like to gain a deeper understanding of imports in Python, particularly absolute and relative imports, then you’ve come to the right place! In this tutorial, you’ll learn the differences between the two, as well as their pros and cons.
00:00 Hello! And welcome to the Real Python guide to Absolute vs Relative Imports in Python. In this set of videos, you’re going to learn the difference between absolute and relative imports as well as when to use each method.
00:13 While this course does assume a bit of knowledge on modules and packages in Python, you’ll first do a quick recap before getting started. Next, you’ll see how absolute imports work, followed by how relative imports work.
00:27
So, why do you import things? Once your projects start getting larger and larger, it can be helpful to split your code into multiple files. Imports allow you to carry functionality between files, including things that you didn’t even write. If you pip install
something, then you have to import it before you can use it. For the scope of this course, a module is a Python file that ends in .py
, while a package is a collection of modules in a folder. The distinction isn’t too critical for this, but it will help when we get to the examples.
00:59
When you import something, Python has a couple of places to look. First, it looks in the sys.modules
cache, which is where everything that was previously imported is kept.
01:10
If something is already in here—maybe you imported it from another script in your project—there’s no need to read another module into the cache. If it’s not there, Python then checks the standard library. This is where things like os
, json
, and csv
are kept.
01:26
Anything that you don’t need to pip install
is generally kept here. Finally, Python will look in the sys.path
. This is a list of directories which usually includes the current directory.
01:39
Any local files that you write for your application should be located here. This is where absolute and relative imports become very important. If Python looks through all these places and still doesn’t find it, then you’ll get the ModuleNotFoundError
, which generally means you either made a typo, didn’t install something, or are directing Python to look in the wrong area.
02:02
Keep in mind that this order is very specific and can have a couple security concerns. sys.modules
is a writeable cache, so there is a chance that unexpected code could be imported into your project.
02:14 If you’re familiar with the DLL hijacking on Windows machines, this is a similar concept and something to keep in mind depending on your application’s use case. To perform an import, you have a couple of different options.
02:27
The most standard way is to just say import
and then put the name of the module that you’d like to bring in. This would bring in all of csv
.
02:36
If you don’t need the entire package or module, you can import a resource directly from it by saying from
and then importing what you need. In this case, Flask
with a capital F
is a class from the flask
package.
02:50
Finally, with whatever you’re importing, you have the option to rename it. So here, while you’re importing all of pandas
, you’re importing it as pd
so that you can refer to it in your code as pd
.
03:04 Finally, PEP 8 has some guidelines for styling your imports.
03:09 Always keep them at the top of your file after any module comments or docstrings
03:14 and then divide them up based on what’s being imported. A good way to break them up is getting anything from the standard library first,
03:23 then any third-party imports that you need, followed by any of your application local imports. Within each of these groups, it’s helpful to keep everything alphabetical so that if there’s a lot of imports, it can be easy to find things later on. And, separate out each of these groups with a blank line. All right! That’s it for the refresher, and now you’re all set to start learning the differences between absolute and relative imports. Thanks for watching.
Dan Bader RP Team on Sept. 16, 2019
Sorry folks, I can’t seem to be able to reproduce this—the videos are playing fine on my end (tested in various browsers). I recommend you check out our “video playback issues” troubleshooting docs.
Looney on Sept. 18, 2019
what can be the use cases where we should add our package in sys.module . ?
Joe Tatusko RP Team on Sept. 18, 2019
Manipulating sys.modules isn’t something you normally have to think about. That is where any module that has been previously imported is stored, so you add modules and packages to it just by importing them.
It might help to think about it this way:
import pandas as pd
print("Pandas imported!")
import pandas as pd
print("Pandas imported again!")
In this example, the first time you import pandas Python is going to look in the sys.modules cache, and not find it (assuming this is the only script that is running). It then looks in the built in modules, and then will look in sys.path for the location of pandas. When you try to import it a second time, Python is able to find it in sys.modules, so it doesn’t need to continue searching or load it again.
Looney on Sept. 19, 2019
Thanks for explanation.
Become a Member to join the conversation.
FP on Sept. 15, 2019
Videos playing but no audio.