Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Absolute Imports in Python

An absolute import specifies the resource to be imported using its full path from the project’s root folder. 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.

In addition, 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.

00:00 Now that you know the basics of importing in Python, it’s time to talk about absolute imports. Performing an absolute import just means that the resource to be imported is identified using its full path from the project’s root folder. For this example, imagine you have a project called projects, and within this project are two packages, package1 and package2. package1 contains a pair of modules, and module2 has a function called function1().

00:32 And when you call this function, all it will say is it’ll just print out that 'This is function1 from module2 which is in package1'. And that way if you’re able to run this, you’ll know that your import was successful. package2 is a little bit different.

00:45 It has an __init__.py file, which contains a class called class1. It also has a pair of modules that don’t have anything in them right now, but it also has a subpackage called subpackage1, which contains module5.

00:58 And this has function2, which like before prints out that 'this is function2 from module5 in subpackage1'. So, this directory is set up to show some of the different possibilities that can happen as a project gets larger and larger. If I were to open up the terminal…

01:18 and let me bring this up, and let’s change directories into projects/ so that if you list everything out, you’ll see package1 and package2.

01:29 And then start the Python interpreter. So if you wanted to import module1, you could say something like from package1 import module1, and that would work.

01:43 And this is telling Python to go ahead and look inside package1, and then find module1 and import this resource.

01:52 But since we don’t have anything here, that’s not too helpful. So let’s look at module2 again and take a look at two different ways to import function1().

02:02 So if you were to do the same thing and say from package1 import module2, now if you try to run function1(), you’re going to run into an error, and that’s because it’s not defined.

02:16 So even though Python knows to look in package1 for module2, you’re importing this module2 as an entire resource.

02:25 If you wanted to call function1(), you would then have to say module2.function1() and call it off of that. It can get annoying to have to type out the module over and over again, so here’s where you could do something like from package1.module2 import function1.

02:48 And now from within module2, you bring function1 into that cache,

02:55 so function1() should work by itself. While this works, it also brings function1 out of module2’s namespace, and it can cause some conflicts.

03:05 This is where you could import something as something else to avoid these issues. So if you did something like from package1 import module2 as, and let’s just say something like mo.

03:20 Now you could do mo.function1() and that would work as well. Now we can mess around with package2, so if you wanted to, you could say from package2 import class1.

03:36 And to make sure this is working, just do something like c = class1(), and that works just like that! So here, because package2 has this __init__ file, anything that’s in here is then imported by importing the package name above.

03:54 Finally, let’s try to get to this module5.function2().

04:00 And just like before, you can say from package2, and then look inside that subpackage1, and then inside module5

04:13 you’re going to want to import function2.

04:18 And just like that, function2() works. And that’s pretty much all there is to absolute imports. The main thing to remember is that wherever you are, you have to define from the project’s root the entire directory structure to get to whatever resource you’re trying to import. PEP 8 recommends absolute imports because they’re pretty clear and straightforward to use.

04:40 Another benefit about them is that if the location you’re calling that import changes, it’ll remain valid. Because of this, you might be thinking “Why not always use absolute imports?” And while it’s true that you should use them as much as possible, depending on your project they can result in very long lines. So for here, you’re looking at a package with a subpackage and a module, and depending on what you’re working for, that might be very shallow.

05:05 You could end up with dozens of subpackages and modules, and at that point, it’s not feasible to write that all the way out for every import. This is where relative imports come into play, and you’ll learn how to use those in the next video. Thanks for watching.

torrepreciado on Jan. 3, 2022

Great explanation!

Become a Member to join the conversation.