Packages in Python | Create, Example

Generally, a group of items or pack of items is called package. In Python, a package is nothing but a folder, or a directory that contains a collection of related multiple modules along with a special file named __init__.py.

In simple words, a package in Python is just a set of Python files (i.e. modules) with a special file __init__.py in a directory structure. The name of directory structure defines the package name.

All the other files within that folder are Python modules. A package may also contain sub-packages and modules. We can add any number of modules into a single package. A simple example of a package is as:

my_package/
   __init__.py
   module1.py
   module2.py
   module3.py

Packages are a group of modules, classes, and data functions. They contain modules, modules contain classes, class contains data and functions. The basic structure of the package is in the below figure.

Structure of packages in Python

As a directory can contain sub-directories and files, packages can also have sub-packages, which are additional directories within the package directory that may contain multiple modules or sub-packages. Sub-packages follow similar structure and import convention as packages.

Let’s take an example to understand it in which we have a package game. This game package consists of sound, image, and level as sub-packages. To create a package, as we discussed above, a file __init__.py must be inside the package. The below picture shows the creation of a package.


Python package structure

Working with packages and modules, we can arrange and structure the code effectively, which makes it more maintainable and reusable.

Types of Packages in Python


There are mainly three types of packages in Python. They are as:

  • Built-in package
  • User-defined package
  • Third-party package

Built-in Packages:

Built-in packages are a collection of pre-written modules that installed on the hard disk of our computer when we install Python. These packages are an integral part of the Python standard library, meaning that are automatically available to us without having to download or install anything separately. We just need to import the package and we can use that specific package.

Built-in packages offer many essential functionalities and tools that we can utilize to perform various tasks efficiently. There are numerous built-in packages available in Python’s standard library.

These packages cover a broad spectrum of functionalities, making Python a versatile language for a wide range of applications. Some of the important built-in packages are as follows:

  • os
  • sys
  • datatime
  • math
  • random
  • json
  • re
  • collections
  • csv
  • urllib

User-defined Package:

User defined packages are those packages that we create in our current project or copy over from other projects to use in our current project. When we create a user defined package in a project, __init__.py file must be present in the package. Let’s understand how to create a user-defined package in Python step by step with the help of example.

How to Create User-defined Package in Python?


There are follow steps to create a package in Python that you should keep in mind. They are as:

(1) Create an empty folder in the Python directory (or Python project) and give it a name as CalculationPack. You make sure the folder should be in the Python directory or current project, otherwise it will not work, as shown in the below picture.

Creating user defined package in Python

(2) Create a text file with .py extension. The name of this file should be __init__ and place it in the CalculationPack package directory. The __init__.py file may be left empty, but we generally place the initialization code for the current package in this file. This file will execute when the package will be imported. Look at the below picture to create __init__.py file.

Creating __init__.py file

Now we have created the __init__.py file and placed it in the package directory, named CalculationPack. Remember that creation of an empty file (__init__.py) is mandatory in the package to recognize the package.

(3) Now, it is the time to create modules in the package directory. In this package, we will create four modules, named addmodule.py, submodule.py, mulmodule.py, and divmodule.py. These modules will be created the same as you created __init__.py file. For example, we will create a module (i.e. file) in which we will write the following code.

Creating a module in package

Give it a name addmodule and save it with extension .py. This module must be placed in the CalculationPack package, as shown in the above picture. Similarly, you also create three more modules with names submodule, mulmodule, and divmodule. Write the following code in each module.

# Code for submodule:
def sub(x, y):
    s = x - y
    return s

# Code for mulmodule:
def multiply(x, y):
    s = x * y
    return s

# Code for divmodule:
def divison(x, y):
    s = x / y
    return s

After creating these modules, place them in the same package, as shown in the above picture. To utilize the CalculationPack package in the Python program, we will need to import it. Let’s understand how to import modules from the package.

How to Import Modules from Package in Python?


To import modules from the package in the Python program, we need to use the package name followed by the module name, separated by dot (.). The general syntax to import modules from the package is as:

<package_name>.<sub_package_name>.<sub_sub_package_name>.<module_name>.<attr_name>

Let’s take an example program in which we will perform operations such as addition, subtraction, multiplication, and division of two numbers using this package. Once we import modules from the package in the Python program, we can easily use its variables and functions using the above syntax. Look at the below code to understand more.

import CalculationPack.addmodule
import CalculationPack.submodule
import CalculationPack.mulmodule
import CalculationPack.divmodule

sum = CalculationPack.addmodule.addition(10, 20)
print(sum)
sub = CalculationPack.submodule.sub(40, 10)
print(sub)
mul = CalculationPack.mulmodule.multiply(20, 30)
print(mul)
div = CalculationPack.divmodule.divison(20, 5)
print(div)
Output:
      30
      30
      600
      4.0

In this example, we have calculated the addition, subtraction, multiplication, and division of two numbers by importing modules of the package. We have passed two numbers to the user-defined functions defined in the modules of the package. After calculation, functions returned values that we have stored in the variables and displayed them on the console.

To import the same package using keyword “as”, write the following code.

import calculationPack.addmodule as pack

To import the same package using keyword “from”, write the following code.

from CalculationPack import submodule
sub = submodule.sub(40, 10)
print(sub)

Third-party Packages in Python


In addition to the standard library, we can also use the third-party packages created by the community in Python. We can install these packages in our system using pip command, which is a package manager for Python packages.

There are various popular third-party packages available in Python that are widely used in a wide range of applications. Some examples of popular third-party packages in Python are as:

  • NumPy
  • SciPy
  • Pandas
  • Matplotlib
  • Seaborn
  • Scikit-learn
  • TensorFlow
  • Django
  • Flask
  • Requests
  • Beautiful Soup and many more.

Advantages of using packages in Python


There are the following advantages of using packages in Python that are as follows:

  • Organizing Python code in packages and modules makes it extremely easy for other developers to work with code.
  • Packages are a good method of separating modules from the other modules to avoid name conflict.
  • They enable us to arrange logical code files, making it more straightforward to manage larger projects.
  • By using a package, we can hide or control the visibility of certain elements of the code from the external access.
  • Packaging code into reusable modules or libraries simplifies code distribution. We can share their packages with others, or within the Python community.

In this tutorial, we have discussed packages in Python with the help of examples. Hope that you will have understood the basic concepts of creating a user-define package and importing of modules from the packages in Python program. In the next, we will discuss math modules in Python with the help of various examples.
Thanks for reading!!!

⇐ PrevNext ⇒

Please share your love