Packages in Python – A Brief Introduction

Filed Under: Python

Today, we’ll learn about packages in Python. Before proceeding to this tutorial you should have knowledge about Python Modules.

What are Packages in Python?

A package in Python is a collection of multiple module files in a single directory. It allows for easier portability with all the application dependencies in a single folder.

We use the dot notation to access the functionality of a module within a package.

For example, if you want to access a module “sample_module” under a package named “sample_package”, you can do so by using sample_package.sample_module.

In short, a Python package makes it easier to work with multiple modules.

How to create a package in Python?

Suppose you want to design a collection of modules for handling the music files. Take a look at the following structure. This is how you’d organize the different files within your package folder. In our case, the top-level package folder is “music”:

music/                          Top-level package
      __init__.py               Initialize the music package
      formats/                  Subpackage for file conversions
              __init__.py
              wavread.py
              wavwrite.py
              aiffread.py
              aiffwrite.py
              auread.py
              auwrite.py
              ...
      effects/                  Subpackage for sound effects
              __init__.py
              echo.py
              surround.py
              reverse.py
              ...
      filters/                  Subpackage for filters
              __init__.py
              equalizer.py
              vocoder.py
              karaoke.py
              ...

Every package in Python needs to have a __init__.py file, which will ensure that this directory will be treated as a Python package.

Generally the __init__.py can be just an empty file or it can also be an executable initialization code for the package or set the __all__ variable which will be explored in the latter part of this tutorial.

To import individual module from the package one can use any of the following ways.

import music.formats.wavwrite

Or

from music.formats import wavwrite

The above statements load the submodule music.formats.wavwrite.

Suppose the module wavwrite.py has a function named writeFile(aFileName) that takes the name of a file as its argument, we call it like the one below:

import music.formats.wavwrite
...
...

music.formats.wavwrite.writeFile(outputFileName)


Or, in the second way-

from music.formats import wavwrite
...
...
wavwrite.writeFile(outputFileName)


We can go even deeper in the import statement where we only import the function we need. Here’s an example of how you can do the same:

from music.formats.wavwrite import writeFile
...
...
writeFile(outputFileName)


How to import all modules from a Python package?

A curious reader may wonder what if one writes from music.formats import * like we did while importing from a module, wouldn’t it be easier?

While it certainly is easier, the issue will be excessive memory usage as most of the functions will not be used within your programs.

The ideal solution is where the package author provides an explicit index of the package.

If a package’s __init__.py code defines a list named __all__, it would be considered as the index of module names that should be imported when from music.formats import * is encountered.

Let’s see a different example to understand this concept clearly. Suppose we have a package structure like this;q

python package tutorial, python package structure example, install python package

Here you can see under \music there is an __init__.py. If the __all__ is defined below;

__all__ = ["admin", "apps", "models"]

Then only submodules enlisted in the above list will be imported while a from music import * is encountered.

If __all__ is not defined the from music import * statement will not import all submodules from the package. The statement from music import * only ensures that the music package has been imported.

Conclusion

So that’s pretty much all of the basic information about python package. For more information you can see the official python doc – https://docs.python.org/3/tutorial/modules.html#packages

So keep practicing. #happy_coding 馃檪

Comments

  1. Pankaj Singh says:

    REGEX parts is not in tutorial…

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content