Python Packages


So far we have learned about Python modules and how different functions, classes, and statements are defined inside them.

In this article, you will learn in detail about Python packages, how different modules are bundled together to create packages, how to import Python packages and many other things.

Python Package: Introduction
How to create a Python package?
How to import a Python package?
 python packages

Python Packages: Introduction

A Python package in simple words is a directory that contains Python files.

Just like a directory has sub-directories and those sub-directories also have files inside, a Python package also has sub-packages and those sub-packages again have different modules defined inside.

A directory with Python files can only be considered as Python package if the directory has a file with name __init__.py.

So every directory having a file __init__.py is considered Python package and the sub-directories having the file __init__.py are considered sub-packages.

The file __init__.py can be empty or contain code to initialize the package as it’s the first file that is executed while importing a package.

How to create a Python Package?

Creating file is like creating directories and dub-directories with a file __init__.py placed inside.

Example 1: let’s create a simple package car containing a simple module display.py.

Here is the structure for creating this package.

python package example

  1. Create a directory car to create a package of the same name.
  2. Create a file __init__.py and place it inside directory car so that it can be considered a Python package.
  3. Create a module inside directory car.The code for module display.py.
    #display.py
    def display():
    print ('This is a car.')
    

Before discussing how to import Python packages, let’s take another example and create a Python package with sub-packages.

Example 2: Create a package named science containing physics, chemistry, and biology as its sub-packages.

Here is the structure.

python package sub-package example

  1. Create a directory science to create a package of the same name.
  2. Create a file __init__.py and place it inside directory science so that it can be considered a Python package.
  3. Create sub-directories physics, chemistry, and biology and place __init__.py inside each sub-directories so that they can be considered Python sub-packages.
  4. Now finally create the corresponding modules inside each sub-packages.physics/display.py
    #display.py
    def display():
    print ('This is physics.')

    chemistry/display.py

    #display.py
    def display():
    print ('This is chemistry.')

    biology/display.py

    #display.py
    def display():
    print ('This is biology.')
    

Now that we have created Python packages, let’s learn about importing them.

How to import Python packages?

We can import Python packages using import statement and dot (.) operator.

If we were to import the package car that we created in example 1, then we have to import it like:

>>> import car

Now to import a function defined inside a module of this package car, here is the way.

>>> from car import display
>>> display.display()  #calling function defined in the module
This is a car.

Another way of importing the module in a package is

>>> import car.display
>>> display.display()
This is a car.

To import Python packages with sub-packages

In example 2, we created a package science with sub-packages. Here is how we import modules and packages in such cases.

>>> import science.physics.display
>>> display.display()
This is physics.

This is the way to import modules from sub-packages.

What if we want to import functions inside the modules defined inside sub-packages?

Here is how it is done.

>>> #importing display function from display module inside sub-package chemistry
>>> from science.chemistry.display import display
>>> display()
This is chemistry.