Why Python Packages?

Last updated on 2025-10-22 | Edit this page

Overview

Questions

  • Why must we package our Python code?
  • What precisely may be packaged?
  • Why is __init_.py file important?
  • What is the Python Package Index (PyPI), and what purpose does it serve?
  • What is a build?

Objectives

  • To understand the benefits of Python packaging.
  • To determine whether packaging is suitable for your use case.
  • To understand the importance of __init_.py file.
  • To acquire knowledge of components of Python packaging and understand Python Package Index (PyPI).
  • To become familiar with the build process.

image Ref : https://xkcd.com/1987/

Introduction


Software development is a creative pursuit, and it is satisfying to write code that solves interesting problems. However, once our code is ready, how can we share it globally? This course provides instruction in how to distribute Python code using Pixi. You might liken a package to the old-style setup.exe: a standardised method of distributing software.

By packaging code, you address several important challenges, including ensuring reproducibility, achieving cross-platform compatibility, and supporting multiple environments. Distributing code as a package offers many advantages over merely sharing source files, for example via GitHub:

Dependency management: A package can explicitly declare its dependencies. Tools such as pip (or uv) can then automatically install them for the user.

Versioning: You may version your package and distribute multiple versions, thereby supporting backwards compatibility.

Standardisation: Packaging is the established method of distributing code via recognised repositories such as PyPI.

Metadata: Packages include project-specific metadata, which is essential for end users.

Python Package Structure Hierarchy


  • Function : A block of reusable code that performs a single specific task.
  • Module : A single Python file (.py) that groups related classes, functions and variables.
  • Package : A folder/ collection containing multiple modules and an init.py file to organize them.
  • The init.py files are required to make Python treat folders containing the file as packages
  • Library : A collection of related packages or modules that provide broad functionality for reuse.
  • Library can have Package(s), which can have module(s) which can have function(s). It can also be considered a Project.
  • Python package : A Python package is a collection of related code modules (files) bundled with metadata describing how the package should be installed and used *
image

GIT PyPI

Steps to create a Python Package :


image

What may be packaged ?


Any .py files (modules) or directories with init.py (packages) can be packaged.

What Is init.Py File in Python?


The init.py file is a Python file that is executed when a package is imported. It serves two main purposes:

  • It marks the directory as a Python Package so that the interpreter can find the modules inside it.

  • It can contain initialization code for the Package, such as importing submodules, defining variables, or executing other code.

    source

What is PyPI


PyPI is the repository where all released Python packages are made available for end users. You may explore it here: PyPI. There is also TestPyPI, a repository which allows us to experiment with distribution tools and procedures without affecting the live PyPI. In this course, we will publish our package to TestPyPI.

What is a Build ?


A build is the process by which your project’s source code is transformed into a distributable format. These build artefacts can then be installed using tools such as pip. A build may be created using the command in the terminal : bash python -m build The successful build process produces files such as a wheel (.whl) or a source archive (.tar.gz), which can be installed via pip or uploaded to PyPI. It is vital to version your build and supply the requisite metadata.

Key Points
  • To make Python code installable, reusable and distributable via PyPI or TestPyPI, one must package the code.
  • The Package should have modules and init.py file.
  • The code must be versioned.
  • Project dependencies must be managed.
  • The project’s metadata must be clearly defined.