Skip to content

Commit f7447bc

Browse files
committed
update packageing chapter
1 parent ef67c08 commit f7447bc

10 files changed

Lines changed: 1033 additions & 4 deletions

File tree

index.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Setting up a Python Project
2929
.. toctree::
3030
:maxdepth: 1
3131

32-
getting_started/virtualenv.rst
32+
python_package/README.rst
3333
getting_started/git_repo.rst
3434
getting_started/structure.rst
3535

@@ -94,10 +94,9 @@ Software Quality
9494
:maxdepth: 1
9595

9696
quality/code_checks.rst
97-
testing/unit_test.rst
97+
testing/README.rst
9898
quality/continuous_integration.rst
99-
testing/facade.rst
100-
99+
101100
Error Handling
102101
--------------
103102

python_package/README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
2+
# Build a Python Package
3+
4+
In this exercise, you will use `uv <https://docs.astral.sh/uv/>`__ to create a package structure for the game `pac` application.
5+
**uv** is a modern tool for managing virtual environments written in rust. It works similar to **pipenv** and **poetry** but is 100x faster when resolving dependencies.
6+
7+
## 1. Installation
8+
9+
Install uv with:
10+
11+
python -m pip install uv
12+
13+
or
14+
15+
curl -LsSf https://astral.sh/uv/install.sh | sh
16+
17+
18+
## 2. Create environment
19+
20+
Create a project folder and execute the commands
21+
22+
uv python install 3.12
23+
uv init
24+
25+
Check what files have been created.
26+
27+
## 3. pyproject.toml
28+
29+
Download the file [pyproject.toml](pyproject.toml).
30+
It contains instructions to install and package the project.
31+
Inspect the file and clarify its contents.
32+
33+
## 4. Install libraries
34+
35+
Install the dependencies listed in `pyproject.toml`:
36+
37+
uv lock
38+
uv sync
39+
40+
The development libraries are installed by default.
41+
42+
You should see a `venv/` folder that contains the installed libraries.
43+
44+
## 5. Add source code
45+
46+
Add a folder `pac/` below the folder containing `pyproject.toml`
47+
Download and add the following two files:
48+
49+
* [pac_game.py](pac_game.py)
50+
* [tiles.png](tiles.png)
51+
52+
Also create a folder `tests/` that we will use later.
53+
54+
## 6. Execute code
55+
56+
Now the program is ready to be executed:
57+
58+
uv run pac/pac_game.py
59+
60+
Also can also use the configuration in `[project.scripts]`:
61+
62+
uv run pacm
63+
64+
Or use the file [__main__.py](__main__.py) and run the python package name:
65+
66+
uv run pac
67+
68+
In all three cases, you should see a graphical window with some starter setup pop up.
69+
70+
71+
## 7. Release the package
72+
73+
Create a distribution with:
74+
75+
uv build
76+
77+
You should find the release files in the `dist/` folder.
78+
79+
If you want to release the sources only, use:
80+
81+
uv build --sdist
82+
83+
## 8. Install the package
84+
85+
The newly created package can be installed with ``pip`` from the release wheel:
86+
87+
pip install dist/pac-1.1.0-py3-none-any.whl
88+
89+
and
90+
91+
python
92+
93+
>>> import pac

python_package/README.rst

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
2+
Building a Python Package
3+
=========================
4+
5+
In this exercise, you will use `uv <https://docs.astral.sh/uv/>`__ to create a package structure for the space game.
6+
**uv** is a modern tool for managing virtual environments written in rust. It works similar to **pipenv** and **poetry** but is 100x faster when resolving dependencies.
7+
8+
Exercise 1: Installation
9+
------------------------
10+
Install uv with:
11+
12+
::
13+
14+
python -m pip install uv
15+
16+
or
17+
18+
::
19+
20+
curl -LsSf https://astral.sh/uv/install.sh | sh
21+
22+
23+
Exercise 2: Create environment
24+
------------------------------
25+
26+
Create a project folder and execute the commands
27+
28+
uv python install 3.12
29+
uv init
30+
31+
Check which files have been created.
32+
33+
Exercise 3: pyproject.toml
34+
--------------------------
35+
36+
Download the file :download:`pyproject.toml`
37+
38+
It contains instructions to install and package a Python project.
39+
Inspect the file and clarify its contents.
40+
41+
The file has been written for a different game - modify what is necessary.
42+
43+
Exercise 4: Install libraries
44+
-----------------------------
45+
46+
Install the dependencies listed in `pyproject.toml`:
47+
48+
::
49+
50+
uv lock
51+
uv sync
52+
53+
The development libraries are installed by default.
54+
55+
You should see a `venv/` folder that contains the installed libraries.
56+
57+
Exercise 5: Add source code
58+
---------------------------
59+
60+
Add a folder `space_game/` below the folder containing `pyproject.toml`
61+
Place the file `space_game.py` there.
62+
63+
Also create a folder `tests/` that we will use later.
64+
65+
Exercise 6: Execute code
66+
------------------------
67+
68+
Now the program is ready to be executed:
69+
70+
::
71+
72+
uv run space_game/space_game.py
73+
74+
Or create a file `space_game/__main__.py` and run the python package name:
75+
76+
::
77+
78+
uv run space_game
79+
80+
In all three cases, the game should start.
81+
82+
83+
Exercise 7: Release the package
84+
-------------------------------
85+
86+
Create a distribution with:
87+
88+
::
89+
90+
uv build
91+
92+
You should find the release files in the `dist/` folder.
93+
94+
If you want to release the sources only, use:
95+
96+
::
97+
98+
uv build --sdist

python_package/__main__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
from pac_game import main
3+
4+
print("Hello Pac")
157 KB
Loading

0 commit comments

Comments
 (0)