|
| 1 | +--- |
| 2 | +title: "Why Python Packages?" |
| 3 | +teaching: 15 |
| 4 | +exercises: 5 |
| 5 | +--- |
| 6 | + |
| 7 | +:::::::::::::::::::::::::::::::::::::: questions |
| 8 | + |
| 9 | +- Why must we package our Python code? |
| 10 | +- What precisely may be packaged? |
| 11 | +- Why is __init_.py file important? |
| 12 | +- What is the Python Package Index (PyPI), and what purpose does it serve? |
| 13 | +- What is a build? |
| 14 | + |
| 15 | + |
| 16 | +:::::::::::::::::::::::::::::::::::::::::::::::: |
| 17 | + |
| 18 | +::::::::::::::::::::::::::::::::::::: objectives |
| 19 | + |
| 20 | +- To understand the benefits of Python packaging. |
| 21 | +- To determine whether packaging is suitable for your use case. |
| 22 | +- To understand the importance of __init_.py file. |
| 23 | +- To acquire knowledge of components of Python packaging and understand Python Package Index (PyPI). |
| 24 | +- To become familiar with the build process. |
| 25 | + |
| 26 | +:::::::::::::::::::::::::::::::::::::::::::::::: |
| 27 | +<img width="983" height="974" alt="image" src="https://github.com/user-attachments/assets/5df6c1a0-bee9-4b38-99e3-725ada1b5d3c" /> |
| 28 | + Ref : https://xkcd.com/1987/ |
| 29 | + |
| 30 | + |
| 31 | +## Introduction |
| 32 | +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. |
| 33 | + |
| 34 | +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: |
| 35 | + |
| 36 | +- Dependency management: A package can explicitly declare its dependencies. Tools such as pip (or uv) can then automatically install them for the user. |
| 37 | +- Versioning: You may version your package and distribute multiple versions, thereby supporting backwards compatibility. |
| 38 | +- Standardisation: Packaging is the established method of distributing code via recognised repositories such as PyPI and conda forge. |
| 39 | +- Metadata: Packages include project-specific metadata, which is essential for end users. |
| 40 | + |
| 41 | +## Python Package Structure Hierarchy |
| 42 | + |
| 43 | +- Function : A block of reusable code that performs a single specific task. |
| 44 | +- Module : A single Python file (.py) that groups related classes, functions and variables. |
| 45 | +- Package : A folder/ collection containing multiple modules and an __init__.py file to organize them. |
| 46 | +- The __init__.py files are required to make Python treat folders containing the file as packages |
| 47 | +- Library : A collection of related packages or modules that provide broad functionality for reuse. |
| 48 | +- Library can have Package(s), which can have module(s) which can have function(s). It can also be considered a Project. |
| 49 | +- 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 [*](https://pydevtools.com/handbook/explanation/what-is-a-python-package/) |
| 50 | + |
| 51 | +<img width="371" height="659" alt="image" src="https://github.com/user-attachments/assets/32c036e1-246a-4f4c-9072-97bfb39376a7" /> |
| 52 | + |
| 53 | + |
| 54 | +[GIT](https://github.com/psf/requests) |
| 55 | +[PyPI](https://pypi.org/project/requests/ ) |
| 56 | + |
| 57 | +## Steps to create a Python Package : |
| 58 | + |
| 59 | +<img width="1298" height="250" alt="image" src="https://github.com/user-attachments/assets/be6f94ec-599b-4a1e-bd5b-63c2dfad720c" /> |
| 60 | + |
| 61 | + |
| 62 | +## What may be packaged ? |
| 63 | + |
| 64 | +Any .py files (modules) or directories with __init__.py (packages) can be packaged. |
| 65 | + |
| 66 | + |
| 67 | +## What is __init__.py file in Python? |
| 68 | +The __init__.py file (aka dunder init) is a Python file that is executed when a package is imported. |
| 69 | +It serves two main purposes: |
| 70 | + |
| 71 | +- It marks the directory as a Python Package so that the interpreter can find the modules inside it. |
| 72 | +- It can contain initialization code for the Package, such as importing submodules, defining variables, or executing other code. |
| 73 | + |
| 74 | + [source](https://www.geeksforgeeks.org/python/what-is-__init__-py-file-in-python/) |
| 75 | + |
| 76 | +## What is PyPI |
| 77 | + |
| 78 | +[PyPI](https://pypi.org/) is the repository where all released Python packages are made available for end users. |
| 79 | +There is also [TestPyPI](https://test.pypi.org/), 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. |
| 80 | +You can search PyPI for existing packages to use in your project instead of creating a new one. To make your package easily discoverable, it’s important to provide correct metadata and appropriate [classifiers](https://pypi.org/classifiers/). |
| 81 | + |
| 82 | +## What is a Build ? |
| 83 | + |
| 84 | +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`. |
| 85 | + A build may be created using the command in the terminal : |
| 86 | + ```bash |
| 87 | +python -m build |
| 88 | +``` |
| 89 | + 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. |
| 90 | + |
| 91 | +**Please note**: There are several tools and backends available for building Python packages. |
| 92 | + |
| 93 | +::::::::::::::::::::::::::::::::::::: keypoints |
| 94 | + |
| 95 | +- To make Python code installable, reusable and distributable via PyPI or TestPyPI, one must package the code. |
| 96 | +- The Package should have modules (.py file(s)) and __init__.py file. |
| 97 | +- The code must be versioned. |
| 98 | +- Project dependencies must be managed. |
| 99 | +- The project’s metadata must be clearly defined. |
| 100 | + |
| 101 | +:::::::::::::::::::::::::::::::::::::::::::::::: |
0 commit comments