|
| 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 |
0 commit comments