Packages in Python – A Brief Introduction

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               Initialize the music package
      formats/                  Subpackage for file conversions
      effects/                  Subpackage for sound effects
      filters/                  Subpackage for filters

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

Generally the 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


from music.formats import wavwrite

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

Suppose the module 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


Or, in the second way-

from music.formats import wavwrite

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

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 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

Packages in Python – A Brief Introduction

Here you can see under \music there is an 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.


So that’s pretty much all of the basic information about python package. For more information you can see the official python doc –

So keep practicing. #happy_coding 🙂

Leave a Reply

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