Skip to content

Commit b429fef

Browse files
committed
source commit: 9ce029c
0 parents  commit b429fef

18 files changed

+1279
-0
lines changed

1_intoduction_python_packaging.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
::::::::::::::::::::::::::::::::::::::::::::::::

2_why_pixi.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
title: "Why choose Pixi for Python Packaging ?"
3+
teaching: 5
4+
exercises: 0
5+
---
6+
7+
:::::::::::::::::::::::::::::::::::::: questions
8+
9+
- Why use Pixi?
10+
- What are the benefits of Pixi ?
11+
12+
::::::::::::::::::::::::::::::::::::::::::::::::
13+
14+
::::::::::::::::::::::::::::::::::::: objectives
15+
16+
- To understand the advantages offered by Pixi.
17+
- To learn about `pixi.toml` and `pyproject.toml`
18+
- To understand the role of `pixi.lock`.
19+
- To explore the concept of multi-environment support.
20+
21+
::::::::::::::::::::::::::::::::::::::::::::::::
22+
23+
## Introduction
24+
25+
Pixi is a fast, modern and reproducible package management tool. It has lots of [features](https://pixi.sh/latest/#what-is-the-difference-with-pixi) which are not all present in a single tool at this point in time. Read [here](https://pixi.sh/latest/concepts/conda_pypi/#tool-comparison). These capabilities make Pixi an attractive choice for managing Python and multi-language projects.
26+
27+
Key features include:
28+
29+
- **Support for both PyPI and Conda packages**: enabling flexibility in sourcing dependencies.
30+
- **Performance**: lightweight and modern, designed for speed.
31+
- **Multi-language dependency management**: e.g. Python with Rust, or Python with C/C++.
32+
- **Integration with `uv`**: leveraging a high-performance package installer.
33+
- **Reproducibility**: guaranteed through the use of `pixi.lock`.
34+
- **Configuration via TOML files**: supports both `pixi.toml` and `pyproject.toml`.
35+
36+
## Configuration files (`pixi.toml` and `pyproject.toml`)
37+
38+
`pixi.toml`: The configuration file used by Pixi to define environments, dependencies, and tasks.
39+
`pyproject.toml`: A standard configuration file within the Python ecosystem (PEP 518/621). It is required by build tools such as Poetry, Hatch, Flit, and setuptools. This file specifies project metadata (e.g. name, version, author) as well as dependencies and build settings.
40+
41+
## Multi-environment support
42+
43+
Pixi allows you to define dependencies for specific operating systems (e.g. Windows, macOS, Linux) or for distinct environments such as development, testing, and production. This makes it easier to tailor the project configuration to match the context in which the software is being deployed or developed.
44+
45+
## pyproject.toml
46+
For this demo, we will mainly focus on `pyproject.toml` file.
47+
We make this choice due to following specifications and recomendations : [PEP 621](https://peps.python.org/pep-0621/), [PEP 517](https://peps.python.org/pep-0517/), and [PEP 660](https://peps.python.org/pep-0660/).
48+
49+
You can also read at these links:
50+
51+
- https://pixi.sh/v0.40.1/reference/pixi_manifest/#pypi-dependencies
52+
- https://pixi.sh/v0.40.1/advanced/pyproject_toml/
53+
54+
## envs
55+
Pixi automatically manages project environments. When you initialize the project with `pixi init`, it creates the project configuration, and the environment is automatically set up when you install dependencies or run tasks.
56+
57+
## dependency management
58+
When you add a package via Pixi (`pixi add <package>`), it updates the project configuration (`pyproject.toml` or `pixi.toml`) and generates or updates the `pixi.lock` file, recording the exact version and source to ensure reproducible environments.
59+
60+
61+
::::::::::::::::::::::::::::::::::::: keypoints
62+
63+
- Choose a tool with good support and long term vision.
64+
- Choose a tool suitable for your project.
65+
- Focus on PEP specifications and recomendations.
66+
67+
::::::::::::::::::::::::::::::::::::::::::::::::

0 commit comments

Comments
 (0)