Absolute vs Relative Imports in Python: Summary
Congratulations! You’re now up to speed on how absolute and relative imports work in Python. 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.
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
00:00 Now you know the difference between absolute and relative imports. In this video, we’re going to quickly recap on the differences between the two and when you would want to use one method over the other. First, an absolute import specifies a resource from the point of view of the project root directory,
00:19
while a relative import is based on where the import
statement is located.
00:25
There are many pros to using absolute imports. First, they’re based on the project root directory, so moving files around means you don’t have to change their import
statements.
00:36 They’re also very readable. Taking a look at an absolute import clearly identifies the resource location. And finally, they’re preferred per PEP 8. The only real con to using an absolute import is they can be very verbose and repetitive based on your project size, so this is something to keep in mind.
00:55 Overall, in most cases you want to use absolute imports as much as possible. Relative imports offer up a couple different advantages. Off the bat, they’re much more concise than absolute imports.
01:07 This advantage is based on your directory structure, so if you organize your modules and packages so that similar resources are located near each other, you can gain a lot by using relative imports. Drawbacks to using relative imports include that they’re not as readable as absolute imports and changing your project can break those imports. Now if you move a file, you often have to change your imports to point to the same resource. Finally, like before, this advantage is really dependent on your directory structure.
01:39 If you have files that are often reaching all the way across your directory for a resource, you may not get as much of a benefit out of using relative imports.
01:48 Generally, you’ll use relative imports when your project is very large with many submodules and packages or you’re using frameworks that are structured in a way that relative imports usually work. And that’s it!
02:01 You should have a solid understanding of the basics between absolute and relative imports. This topic can be a bit tricky to understand, so the best way to learn is to go ahead and practice.
02:12 Try using both methods in your next project and see which one works out better for you. Thanks for watching.
Chasp on Sept. 11, 2019
Succinct and understandable… good job!
abakala on Nov. 19, 2019
Thanks, it was very helpful!
Sachin on Dec. 17, 2019
Nice, clear and simple explanation!! This is first time I have understood this!!
Priya katta on Dec. 20, 2019
very helpful…thanks!
Pakorn on Jan. 1, 2020
Good job. Thanks
Crystal Taggart on May 18, 2020
I’d recommend adding a video for this command for relative imports:
import sys sys.path.append(“..”)
Alan ODannel on July 15, 2020
Very helpful. I put this to immediate use cleaning up a Python application that I inherited at wok.
Sid Price on Oct. 12, 2020
Really enjoyed the import course!
I have a project folder structure and import situation I do not seem to be able to resolve, so, hoping you can help me.
I have:
Project Folder:
App Folder named 'app'
Widgets Folder named "widgets'
In the app folder I am developing a dialog widget and I would like to test it standalone. It has code in it that when the module is the “main” module it creates an application object and instantiates the dialog for testing.
The issue I face is that the dialog widget requires access to modules in the “App Folder” and I cannot figure out how to do that.
You advice would be much appreciated. Sid
Ghani on Oct. 26, 2020
Very good course; thanks!
Aldir on Dec. 21, 2021
Well done Joe. Short and to the point!! Thank you
Become a Member to join the conversation.
aradim on Sept. 11, 2019
Thanks , very helpfull