Introduction

One of the fundamental aspects of Python is its module system, which allows for the creation of reusable code. A package is a collection of modules that can be easily shared, reused, and extended. In this tutorial, we will learn how to create packages in Python. We will explore the different types of packages, how to structure the package, and how to publish and install the package using PyPI. By the end of this tutorial, you will have a comprehensive understanding of how to create and distribute your own Python packages.

Table of Contents :

  • How to Create Packages in Python
  • Working of __init__.py
  • Importing Modules from a Package
  • Importing Specific Function from the Module of a python Package 
  • Importing Packages 
  • Installing Packages 
  • Creating a Requirements File 

How to Create Packages in Python :

  • A package is a collection of modules.
  • Put simply, a package is a directory of Python modules. 
  • Creating a package is as simple as creating a directory with a __init__.py file inside of it. 
  • The __init__.py file can be empty, or it can execute initialization code for the package. 
  • Packages can be created by turning an ordinary directory into a package by adding a special file called `__init__.py` to it.
  • The package directory should have a unique name that is unrelated to any module names.
  • Modules can be placed directly in the package directory, or in subdirectories within the package directory.
  • Example:

# my_package/__init__.py
from . import module1
from .sub_package import module2
__all__ = ["module1", "module2"]

 __init__.py file of a python package

  • The `__init__.py` file is executed when the package or module is imported.
  • It can be used to specify which modules are intended to be part of the package, using the `__all__` attribute.
  • It can also contain code to execute as part of the package initialization process.

API of a python package

  • The  __init__.py  file can also be used to define the API of a package. 
  • The API is defined by the variables and functions that are defined in the  __init__.py  file. 

Data Files

  • Packages can also contain data files. 
  • Data files are files that are used by a package, but are not part of the package’s API. 
  • Data files are typically located in the package’s data directory. 

import statement

Importing Modules from a Package :

  • To import modules from a package, use the `import` statement followed by the package name and module name separated by a dot.
  • Example:

# main.py
import my_package.module1
import my_package.sub_package.module2
my_package.module1.greeting("John")
my_package.sub_package.module2.farewell("John")

Importing Specific Function from the Module of a python Package :

  • To import specific functions from a module within a package, use the `from` keyword followed by the package name, module name, and function name.
  • Example:

# main.py
from my_package.module1 import greeting
greeting("John")

Importing Packages 

  • Importing a package is done with the import keyword. 
  • For example, if you have a package named mypackage, you can import it like this: import mypackage 
  • This will make the mypackage package available in your code. 
  • You can then access the functions, classes, and variables defined in mypackage. 

Installing Packages 

  • If you want to use a Python package that's not part of the standard library, you'll need to install it. 
  • The easiest way to install a package is with the pip tool. 
  • pip is a package manager for Python. 
  • It's used to install and manage packages from the Python Package Index (PyPI). 
  • To install a package with pip, use the following command: pip install package_name Replace package_name with the name of the package you want to install. 

Creating a Requirements File 

  • If you're working on a project that uses multiple Python packages, it's a good idea to create a requirements file. 
  • A requirements file is a text file that lists the packages your project needs to run. 
  • Creating a requirements file is simple. 
  • Just use the pip freeze command to generate a list of installed packages: pip freeze > requirements.txt 
  • This will create a file named requirements.txt in the current directory. 
  • The file will contain a list of all the packages installed on your system. 
  • You can then use the pip install -r command to install all the packages in the requirements file: pip install -r requirements.txt

Prev. Tutorial : Packages

Next Tutorial : module Vs package