Python import Example

1. Introduction

Programming languages have a lot of features that improve our code making it stable and reusable, speeding up the development of the application.

Python uses the import statement to make use of other modules into our code that can be accessed throughout the implementation of our solution.

In the next step, we’re going to see the different kinds of importing and learning about packages and modules embedded in Python and so on.

2. Packages in Python

In a few words, packages are a directory with Python files (modules) that we use in our code. Further, we could choose between using an entire package or just a module present in this package.

Packages are a way of structuring Python’s module namespace by using “dotted module names”. For example, the module name A.B designates a submodule named B in a package named A

Let’s make a practical example by creating a directory named pkg and putting two Python code files into it.

Creating pkg directory

$ mkdir pkg

def foo():
    print('[mod1] foo()')

class Foo:

def bar():
    print('[mod2] bar()')

class Bar:

Now, open Python’s REPL on the terminal, we can import the package that we just have created.

Importing package

$ python3
Python 3.9.1 (default, Jan 30 2021, 15:51:05) 
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pkg.mod1, pkg.mod2
[mod1] foo()
>>> x = pkg.mod2.Bar()
>>> x

Alternatively, we can use the from statement to import individual objects into our code.

from statement

>>> from pkg import mod1
[mod1] foo()

3. Absolute imports

With the examples shown earlier, we’ve used the absolute import that specifies the resource to be imported into our code. That means, we specify the path where our required package is to be accessed by the code just using Python’s module namespace (dot notation).

Let’s see this structure on our project:

Structure example for absolute import

└── project
    ├── package1
    │   ├──
    │   └──
    └── package2
        └── subpackage1

Using absolute import, if we want to import the module1 from package1, should look like this

Absolute import example 1

from package1 import module1

Now, suppose that we want a function1 from module2 in package1 and a class1 from package2.

Absolute import example 2

from package1.module2 import function1
from package2 import class1

That’s how absolute import works in Python.

Pros: absolute imports are preferred due to being clear and straightforward, making easier what resource we want to use in our code. Also, is the PEP 8 (Python Enhancement Proposal) recommendation to maintain a clean implementation using Python language.

Cons: depending on the complexity of the importing, absolute import gets verbose sometimes.

4. Relative imports

On the other hand, a relative import specifies the resource to be imported relative to the current location. That means the location where the import statement is.

A single leading dot indicates a relative import, starting with the current package. Two or more leading dots indicate a relative import to the parent(s) of the current package, one level per dot after the first.

With our previous structure example, let’s import function1 into the package1/

Relative import example

# package1/

from .module2 import function1

We just need to use one dot because is in the same directory as

Pros: relative imports are quite succinct compared with absolute imports. Therefore, depending on the current location of your project, it would be easier to make your imports.

Cons: it can be a problem depending on your project structure.

5. Conclusion

In summary, we discuss the Python import system. Further, we had an explanation about packages and how they’re acceded by Python code.

Finally, we discovered the differences between Absolute and Relative imports together with their pros and counters.

This article was based on Python’s official documentation that can find here. Also, I used Real Python’s article about imports as an inspiration that you can read on this link.

6. Download the source code

You can download the full source code of this example here: Python import Example

Sergio Lauriano Junior

Sergio is graduated in Software Development in the University City of São Paulo (UNICID). During his career, he get involved in a large number of projects such as telecommunications, billing, data processing, health and financial services. Currently, he works in financial area using mainly Java and IBM technologies.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button