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
Let’s make a practical example by creating a directory named
pkg and putting two Python code files into it.
Creating pkg directory
$ mkdir pkgmod1.py
def foo(): print('[mod1] foo()') class Foo: passmod2.py
def bar(): print('[mod2] bar()') class Bar: pass
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-122.214.171.124)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import pkg.mod1, pkg.mod2 >>> pkg.mod1.foo() [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() [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 │ ├── module1.py │ └── module2.py └── package2 ├── __init__.py ├── module3.py ├── module4.py └── subpackage1 └── module5.py
Using absolute import, if we want to import the
package1, should look like thisAbsolute import example 1
from package1 import module1
Now, suppose that we want a
package1 and a
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/module1.pyRelative import example
# package1/module1.py from .module2 import function1
We just need to use one dot because
module1.py 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.
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.
6. Download the source code
You can download the full source code of this example here: Python import Example