Skip to content
Myr

📝 Update the "Building a Package" tutorial to use `uv` instead of `poetry` (#1474)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sofie Van Landeghem · committed by GitHub · parent 2ace760 · Browse files · 1 file+98 added−182 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>

Diff stats

1 file+98 added−182 removed

  • other:+98 added−182 removed

1 file+98 added−182 removed

modified:docs/tutorial/package.md+98 added-182 removed
2 unmodified lines
3When you create a CLI program with **Typer** you probably want to create your own Python package.45That's what allows your users to install it and have it as an independent program that they can use in their terminal.67And that's also required for shell auto completion to work (unless you use your program through `typer` command).7And that's also required for shell auto completion to work (unless you use your program through the `typer` command).89Nowadays, there are several ways and tools to create Python packages (what you install with `pip install something`).9Nowadays, there are several ways and tools to create Python packages (what you install with `pip install something` or `uv add something`).1011You might even have your favorite already.1213Here's a very opinionated, short guide, showing one of the alternative ways of creating a Python package with a **Typer** app, from scratch.
5 unmodified lines
19///2021## Prerequisites2223For this guide we'll use <a href="https://python-poetry.org/" class="external-link" target="_blank">Poetry</a>.23For this guide we'll use <a href="https://docs.astral.sh/uv/" class="external-link" target="_blank">uv</a>.2425Poetry's docs are great, so go ahead, check them and install it.25uv's docs are great, so go ahead, check them and install it.2627## Create a project2829Let's say we want to create a CLI application called `portal-gun`.
1 unmodified line
31To make sure your package doesn't collide with the package created by someone else, we'll name it with a prefix of your name.3233So, if your name is Rick, we'll call it `rick-portal-gun`.3435Create a project with Poetry:35Create a project with uv:3637<div class="termy">3839```console40$ poetry new rick-portal-gun40$ uv init --package rick-portal-gun4142Created package rick_portal_gun in rick-portal-gun42Initialized project `rick-portal-gun` at `/home/rick-portal-gun`4344// Enter the new project directory45cd ./rick-portal-gun46```
6 unmodified lines
5354<div class="termy">5556```console57$ poetry add typer57$ uv add typer5859// It creates a virtual environment for your project60Creating virtualenv rick-portal-gun-w31dJa0b-py3.10 in /home/rick/.cache/pypoetry/virtualenvs61Using version ^0.12.0 for typer60Using CPython 3.14.0 interpreter at: /location/of/python/61Creating virtual environment at: .venv6263Updating dependencies64Resolving dependencies... (1.2s)63Resolved 10 packages in 21ms64 Built rick-portal-gun @ file:/home/rick-portal-gun65Prepared 1 package in 19ms66Installed 10 packages in 34ms67 + click==8.3.168 + colorama==0.4.669 + markdown-it-py==4.0.070 + mdurl==0.1.271 + pygments==2.19.272 + rich==14.2.073 + rick-portal-gun==0.1.0 (from file:/home/rick-portal-gun)74 + shellingham==1.5.475 + typer==0.21.076 + typing-extensions==4.15.07766---> 100%7868Package operations: 8 installs, 0 updates, 0 removals6970 - Installing mdurl (0.1.2)71 - Installing markdown-it-py (3.0.0)72 - Installing pygments (2.17.2)73 - Installing click (8.1.7)74 - Installing rich (13.7.1)75 - Installing shellingham (1.5.4)76 - Installing typing-extensions (4.11.0)77 - Installing typer (0.12.3)7879Writing lock file8079// Activate that new virtual environment82$ poetry shell80$ source .venv/bin/activate8184Spawning shell within /home/rick/.cache/pypoetry/virtualenvs/rick-portal-gun-w31dJa0b-py3.108582// Open an editor using this new environment, for example VS Code83$ code ./84```85
2 unmodified lines
88You can see that you have a generated project structure that looks like:8990```91.96├── poetry.lock92├── pyproject.toml93├── README.md99├── rick_portal_gun100│   └── __init__.py101└── tests102 └── __init__.py94├── src95│   └── rick_portal_gun96│     └── __init__.py97└── uv.lock98```99100## Create your app101102Now let's create an extremely simple **Typer** app.103109Create a file `rick_portal_gun/main.py` with:104Create a file `src/rick_portal_gun/main.py` with:105106```Python107import typer108
41 unmodified lines
150```151152## Add a "script"153159We are creating a Python package that can be installed with `pip install`.154We are creating a Python package that can be installed with `uv add` or `pip install`.155156But we want it to provide a CLI program that can be executed in the shell.157163To do that, we add a configuration to the `pyproject.toml` in the section `[tool.poetry.scripts]`:158To do that, we add a configuration to the `pyproject.toml` in the section `[project.scripts]`:159165```TOML hl_lines="8 9"166[tool.poetry]160```TOML hl_lines="12 13"161[project]162name = "rick-portal-gun"163version = "0.1.0"169description = ""170authors = ["Rick Sanchez <rick@example.com>"]164description = "Add your description here"165readme = "README.md"166authors = ["Rick Sanchez <rick@example.com>"]167requires-python = ">=3.14"168dependencies = [169 "typer>=0.21.0",170]171173[tool.poetry.scripts]172[project.scripts]173rick-portal-gun = "rick_portal_gun.main:app"174176[tool.poetry.dependencies]177python = "^3.10"178typer = "^0.12.0"179175[build-system]181requires = ["poetry-core"]182build-backend = "poetry.core.masonry.api"176requires = ["uv_build>=0.8.14,<0.9.0"]177build-backend = "uv_build"178```179180Here's what that line means:181
21 unmodified lines
203from rick_portal_gun.main import app204app()205```206212That config section tells Poetry that when this package is installed we want it to create a command line program called `rick-portal-gun`.207That config section tells uv that when this package is installed, we want it to create a command line program called `rick-portal-gun`.208209And that the object to call (like a function) is the one in the variable `app` inside of the module `rick_portal_gun.main`.210211## Install your package
4 unmodified lines
216217<div class="termy">218219```console225$ poetry install220$ uv sync221227Installing dependencies from lock file222Resolved 10 packages in 1ms223 Built rick-portal-gun @ file:/home/rick-portal-gun224Prepared 1 package in 18ms225Uninstalled 1 package in 1ms226Installed 1 package in 13ms227 ~ rick-portal-gun==0.1.0 (from file:/home/rick-portal-gun)228229No dependencies to install or update230231 - Installing the current project: rick-portal-gun (0.1.0)229```230231</div>232233## Try your CLI program234238Your package is installed in the environment created by Poetry, but you can already use it.235Your package is installed in the environment created by uv, but you can already use it.236237<div class="termy">238239```console240// You can use the which program to check which rick-portal-gun program is available (if any)241$ which rick-portal-gun242243// You get the one from your environment247/home/rick/.cache/pypoetry/virtualenvs/rick-portal-gun-w31dJa0b-py3.10/bin/rick-portal-gun244/home/rick-portal-gun/.venv/bin/rick-portal-gun245246// Try it247$ rick-portal-gun --help248
8 unmodified lines
257258 --help Show this message and exit.259260Commands:264 load Load the portal gun261 shoot Shoot the portal gun262 load Load the portal gun263```264265</div>266267## Create a wheel package268269Python packages have a standard format called a "wheel". It's a file that ends in `.whl`.270274You can create a wheel with Poetry:271You can create a wheel with uv:272273<div class="termy">274275```console279$ poetry build276$ uv build277281Building rick-portal-gun (0.1.0)282 - Building sdist283 - Built rick-portal-gun-0.1.0.tar.gz284 - Building wheel285 - Built rick_portal_gun-0.1.0-py3-none-any.whl278Building source distribution (uv build backend)...279Building wheel from source distribution (uv build backend)...280Successfully built dist/rick_portal_gun-0.1.0.tar.gz281Successfully built dist/rick_portal_gun-0.1.0-py3-none-any.whl282```283284</div>285
34 unmodified lines
320///321322/// tip323328Bonus points if you use <a href="https://github.com/pipxproject/pipx" class="external-link" target="_blank">`pipx`</a> to install it while keeping an isolated environment for your Python CLI programs 🚀324Bonus points if you use <a href="https://docs.astral.sh/uv/" class="external-link" target="_blank">uvx</a> to install it while keeping an isolated environment for your Python CLI programs 🚀325326///327328Now you have your CLI program installed. And you can use it freely:
81 unmodified lines
410You can support that same style of calling the package/module for your own package, simply by adding a file `__main__.py`.411412Python will look for that file and execute it.413418The file would live right beside `__init__.py`:414The file would live right beside `__init__.py` and `main.py`:415416``` hl_lines="7"417.422├── poetry.lock418├── pyproject.toml419├── README.md425├── rick_portal_gun426│ ├── __init__.py427│ ├── __main__.py428│ └── main.py429└── tests430 └── __init__.py420├── src421│   └── rick_portal_gun422│     ├── __init__.py423│     ├── __main__.py424│     └── main.py425└── uv.lock426```427428No other file has to import it, you don't have to reference it in your `pyproject.toml` or anything else, it just works by default, as it is standard Python behavior.429
3 unmodified lines
433from .main import app434app()435```436442Now, after installing your package, if you call it with `python -m` it will work (for the main part):437Now, after installing your package, if you call it with `python -m` it will work:438439<div class="termy">440441```console442$ python -m rick_portal_gun --help443449Usage: __main__.py [OPTIONS] COMMAND [ARGS]...444Usage: python -m rick_portal_gun [OPTIONS] COMMAND [ARGS]...445446 Awesome Portal Gun447448Options:
2 unmodified lines
451452 --help Show this message and exit.453454Commands:460 load Load the portal gun455 shoot Shoot the portal gun456 load Load the portal gun457```458459</div>460
2 unmodified lines
463Notice that you have to pass the importable version of the package name, so `rick_portal_gun` instead of `rick-portal-gun`.464465///466472That works! 🚀 Sort of... 🤔467That works! 🚀468474See the `__main__.py` in the help instead of `rick-portal-gun`? We'll fix that next.475476### Set a program name in `__main__.py`477478We are setting the program name in the file `pyproject.toml` in the line like:479480```TOML481[tool.poetry.scripts]482rick-portal-gun = "rick_portal_gun.main:app"483```484485But when Python runs our package as a script with `python -m`, it doesn't have the information of the program name.486487So, to fix the help text to use the correct program name when called with `python -m`, we can pass it to the app in `__main__.py`:488489```Python490from .main import app491app(prog_name="rick-portal-gun")492```493494/// tip495496You can pass all the arguments and keyword arguments you could pass to a Click application, including `prog_name`.497498///499500<div class="termy">501502```console503$ python -m rick_portal_gun --help504505Usage: rick-portal-gun [OPTIONS] COMMAND [ARGS]...506507 Awesome Portal Gun508509Options:510 --install-completion Install completion for the current shell.511 --show-completion Show completion for the current shell, to copy it or customize the installation.512513 --help Show this message and exit.514515Commands:516 load Load the portal gun517 shoot Shoot the portal gun518```519520</div>521522Great! That works correctly! 🎉 ✅523524Notice that now it uses `rick-portal-gun` instead of `__main__.py` in the help.525469### Autocompletion and `python -m`470471Have in mind that TAB completion (shell auto-completion) won't work when using `python -m`.472
30 unmodified lines
503```504pypi-wubalubadubdub-deadbeef1234505```506564Now configure Poetry to use this token with the command `poetry config pypi-token.pypi`:507Now configure uv to use this token by setting an environment variable:508509<div class="termy">510511```console569$ poetry config pypi-token.pypi pypi-wubalubadubdub-deadbeef1234512$ export UV_PUBLISH_TOKEN=pypi-wubalubadubdub-deadbeef1234513// It won't show any output, but it's already configured514```515516</div>517518### Publish to PyPI519577Now you can publish your package with Poetry.520Now you can publish your package.521579You could build the package (as we did above) and then publish later, or you could tell poetry to build it before publishing in one go:580522<div class="termy">523524```console584$ poetry publish --build525$ uv publish526586# There are 2 files ready for publishing. Build anyway? (yes/no) [no] $ yes587588---> 100%589590Building rick-portal-gun (0.1.0)591 - Building sdist592 - Built rick-portal-gun-0.1.0.tar.gz593 - Building wheel594 - Built rick_portal_gun-0.1.0-py3-none-any.whl595596Publishing rick-portal-gun (0.1.0) to PyPI597 - Uploading rick-portal-gun-0.1.0.tar.gz 100%598 - Uploading rick_portal_gun-0.1.0-py3-none-any.whl 100%527Publishing 2 files https://upload.pypi.org/legacy/528Uploading rick_portal_gun-0.1.0-py3-none-any.whl (2.3KiB)529Uploading rick_portal_gun-0.1.0.tar.gz (841.0B)530```531532</div>533
2 unmodified lines
536You should now see your new "rick-portal-gun" package.537538### Install from PyPI539609Now to see that we can install it form PyPI, open another terminal, and uninstall the currently installed package.540Now to see that we can install it from PyPI, open another terminal, and uninstall the currently installed package.541542<div class="termy">543544```console
79 unmodified lines
624625For that you need to first increase the version in `pyproject.toml`:626627```TOML hl_lines="3"697[tool.poetry]628[project]629name = "rick-portal-gun"630version = "0.2.0"700description = ""701authors = ["Rick Sanchez <rick@example.com>"]631description = "Add your description here"632readme = "README.md"633authors = ["Rick Sanchez <rick@example.com>"]634requires-python = ">=3.14"635dependencies = [636 "typer>=0.21.0",637]638704[tool.poetry.scripts]639[project.scripts]640rick-portal-gun = "rick_portal_gun.main:app"641707[tool.poetry.dependencies]708python = "^3.10"709typer = "^0.12.0"710642[build-system]712requires = ["poetry-core"]713build-backend = "poetry.core.masonry.api"643requires = ["uv_build>=0.8.14,<0.9.0"]644build-backend = "uv_build"645```646716And in the file `rick_portal_gun/__init__.py`:717718```Python719__version__ = '0.2.0'720```721647And then build and publish again:648649<div class="termy">650651```console727$ poetry publish --build652$ uv build653$ uv publish654729---> 100%730731Building rick-portal-gun (0.2.0)732 - Building sdist733 - Built rick-portal-gun-0.2.0.tar.gz734 - Building wheel735 - Built rick_portal_gun-0.2.0-py3-none-any.whl736737Publishing rick-portal-gun (0.2.0) to PyPI738 - Uploading rick-portal-gun-0.2.0.tar.gz 100%739 - Uploading rick_portal_gun-0.2.0-py3-none-any.whl 100%655Publishing 2 files https://upload.pypi.org/legacy/656Uploading rick_portal_gun-0.2.0-py3-none-any.whl (2.3KiB)657Uploading rick_portal_gun-0.2.0.tar.gz (840.0B)658```659660</div>661
4 unmodified lines
666This is a very simple guide. You could add many more steps.667668For example, you should use <a href="https://git-scm.com/" class="external-link" target="_blank">Git</a>, the version control system, to save your code.669752You can add a lot of extra metadata to your `pyproject.toml`, check the docs for <a href="https://python-poetry.org/docs/libraries/" class="external-link" target="_blank">Poetry: Libraries</a>.670You could use <a href="https://docs.astral.sh/uv/" class="external-link" target="_blank">uv</a> to manage your installed CLI Python programs in isolated environments.671754You could use <a href="https://github.com/pipxproject/pipx" class="external-link" target="_blank">`pipx`</a> to manage your installed CLI Python programs in isolated environments.672Maybe use automatic formatting with <a href="https://docs.astral.sh/ruff/" class="external-link" target="_blank">Ruff</a>.673756Maybe use automatic formatting with <a href="https://github.com/psf/black" class="external-link" target="_blank">Black</a>.757674You'll probably want to publish your code as open source to <a href="https://github.com/" class="external-link" target="_blank">GitHub</a>.675676And then you could integrate a <abbr title="Continuous Integration">CI</abbr> tool to run your tests and deploy your package automatically.677762And there's a long etc. But now you have the basics and you can continue on your own 🚀.678And there's a long etc. But now you have the basics and you can continue on your own. 🚀