📝 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
Provenance
- Commit
7c12035- Tree
f05389e- Author
Sofie Van Landeghem <svlandeg@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
2ace760
1 file changed+98 added−182 removed
modified:docs/tutorial/package.md+98 added-182 removed
2 unmodified lines3 When you create a CLI program with **Typer** you probably want to create your own Python package.4 5 That's what allows your users to install it and have it as an independent program that they can use in their terminal.6 7-And that's also required for shell auto completion to work (unless you use your program through `typer` command).7+And that's also required for shell auto completion to work (unless you use your program through the `typer` command).8 9-Nowadays, there are several ways and tools to create Python packages (what you install with `pip install something`).9+Nowadays, there are several ways and tools to create Python packages (what you install with `pip install something` or `uv add something`).10 11 You might even have your favorite already.12 13 Here'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 lines19 ///20 21 ## Prerequisites22 23-For this guide we'll use <a href="https://python-poetry.org/" class="external-link" target="_blank">Poetry</a>.23+For this guide we'll use <a href="https://docs.astral.sh/uv/" class="external-link" target="_blank">uv</a>.24 25-Poetry's docs are great, so go ahead, check them and install it.25+uv's docs are great, so go ahead, check them and install it.26 27 ## Create a project28 29 Let's say we want to create a CLI application called `portal-gun`.1 unmodified line31 To make sure your package doesn't collide with the package created by someone else, we'll name it with a prefix of your name.32 33 So, if your name is Rick, we'll call it `rick-portal-gun`.34 35-Create a project with Poetry:35+Create a project with uv:36 37 <div class="termy">38 39 ```console40-$ poetry new rick-portal-gun40+$ uv init --package rick-portal-gun41 42-Created package rick_portal_gun in rick-portal-gun42+Initialized project `rick-portal-gun` at `/home/rick-portal-gun`43 44 // Enter the new project directory45 cd ./rick-portal-gun46 ```6 unmodified lines53 54 <div class="termy">55 56 ```console57-$ poetry add typer57+$ uv add typer58 59 // It creates a virtual environment for your project60-Creating virtualenv rick-portal-gun-w31dJa0b-py3.10 in /home/rick/.cache/pypoetry/virtualenvs61-Using version ^0.12.0 for typer60+Using CPython 3.14.0 interpreter at: /location/of/python/61+Creating virtual environment at: .venv62 63-Updating dependencies64-Resolving dependencies... (1.2s)63+Resolved 10 packages in 21ms64+ Built rick-portal-gun @ file:/home/rick-portal-gun65+Prepared 1 package in 19ms66+Installed 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.077 66----> 100%78 68-Package operations: 8 installs, 0 updates, 0 removals69-70- - 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)78-79-Writing lock file80-79 // Activate that new virtual environment82-$ poetry shell80+$ source .venv/bin/activate81 84-Spawning shell within /home/rick/.cache/pypoetry/virtualenvs/rick-portal-gun-w31dJa0b-py3.1085-82 // Open an editor using this new environment, for example VS Code83 $ code ./84 ```85 2 unmodified lines88 You can see that you have a generated project structure that looks like:89 90 ```91 .96-├── poetry.lock92 ├── pyproject.toml93 ├── README.md99-├── rick_portal_gun100-│ └── __init__.py101-└── tests102- └── __init__.py94+├── src95+│ └── rick_portal_gun96+│ └── __init__.py97+└── uv.lock98 ```99 100 ## Create your app101 102 Now let's create an extremely simple **Typer** app.103 109-Create a file `rick_portal_gun/main.py` with:104+Create a file `src/rick_portal_gun/main.py` with:105 106 ```Python107 import typer108 41 unmodified lines150 ```151 152 ## Add a "script"153 159-We are creating a Python package that can be installed with `pip install`.154+We are creating a Python package that can be installed with `uv add` or `pip install`.155 156 But we want it to provide a CLI program that can be executed in the shell.157 163-To do that, we add a configuration to the `pyproject.toml` in the section `[tool.poetry.scripts]`:158+To do that, we add a configuration to the `pyproject.toml` in the section `[project.scripts]`:159 165-```TOML hl_lines="8 9"166-[tool.poetry]160+```TOML hl_lines="12 13"161+[project]162 name = "rick-portal-gun"163 version = "0.1.0"169-description = ""170-authors = ["Rick Sanchez <rick@example.com>"]164+description = "Add your description here"165 readme = "README.md"166+authors = ["Rick Sanchez <rick@example.com>"]167+requires-python = ">=3.14"168+dependencies = [169+ "typer>=0.21.0",170+]171 173-[tool.poetry.scripts]172+[project.scripts]173 rick-portal-gun = "rick_portal_gun.main:app"174 176-[tool.poetry.dependencies]177-python = "^3.10"178-typer = "^0.12.0"179-175 [build-system]181-requires = ["poetry-core"]182-build-backend = "poetry.core.masonry.api"176+requires = ["uv_build>=0.8.14,<0.9.0"]177+build-backend = "uv_build"178 ```179 180 Here's what that line means:181 21 unmodified lines203 from rick_portal_gun.main import app204 app()205 ```206 212-That config section tells Poetry that when this package is installed we want it to create a command line program called `rick-portal-gun`.207+That config section tells uv that when this package is installed, we want it to create a command line program called `rick-portal-gun`.208 209 And that the object to call (like a function) is the one in the variable `app` inside of the module `rick_portal_gun.main`.210 211 ## Install your package4 unmodified lines216 217 <div class="termy">218 219 ```console225-$ poetry install220+$ uv sync221 227-Installing dependencies from lock file222+Resolved 10 packages in 1ms223+ Built rick-portal-gun @ file:/home/rick-portal-gun224+Prepared 1 package in 18ms225+Uninstalled 1 package in 1ms226+Installed 1 package in 13ms227+ ~ rick-portal-gun==0.1.0 (from file:/home/rick-portal-gun)228 229-No dependencies to install or update230-231- - Installing the current project: rick-portal-gun (0.1.0)229 ```230 231 </div>232 233 ## Try your CLI program234 238-Your package is installed in the environment created by Poetry, but you can already use it.235+Your package is installed in the environment created by uv, but you can already use it.236 237 <div class="termy">238 239 ```console240 // You can use the which program to check which rick-portal-gun program is available (if any)241 $ which rick-portal-gun242 243 // 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-gun245 246 // Try it247 $ rick-portal-gun --help248 8 unmodified lines257 258 --help Show this message and exit.259 260 Commands:264- load Load the portal gun261 shoot Shoot the portal gun262+ load Load the portal gun263 ```264 265 </div>266 267 ## Create a wheel package268 269 Python packages have a standard format called a "wheel". It's a file that ends in `.whl`.270 274-You can create a wheel with Poetry:271+You can create a wheel with uv:272 273 <div class="termy">274 275 ```console279-$ poetry build276+$ uv build277 281-Building 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.whl278+Building source distribution (uv build backend)...279+Building wheel from source distribution (uv build backend)...280+Successfully built dist/rick_portal_gun-0.1.0.tar.gz281+Successfully built dist/rick_portal_gun-0.1.0-py3-none-any.whl282 ```283 284 </div>285 34 unmodified lines320 ///321 322 /// tip323 328-Bonus 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 🚀324+Bonus 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 🚀325 326 ///327 328 Now you have your CLI program installed. And you can use it freely:81 unmodified lines410 You can support that same style of calling the package/module for your own package, simply by adding a file `__main__.py`.411 412 Python will look for that file and execute it.413 418-The file would live right beside `__init__.py`:414+The file would live right beside `__init__.py` and `main.py`:415 416 ``` 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 ```427 428 No 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 lines433 from .main import app434 app()435 ```436 442-Now, after installing your package, if you call it with `python -m` it will work (for the main part):437+Now, after installing your package, if you call it with `python -m` it will work:438 439 <div class="termy">440 441 ```console442 $ python -m rick_portal_gun --help443 449-Usage: __main__.py [OPTIONS] COMMAND [ARGS]...444+Usage: python -m rick_portal_gun [OPTIONS] COMMAND [ARGS]...445 446 Awesome Portal Gun447 448 Options:2 unmodified lines451 452 --help Show this message and exit.453 454 Commands:460- load Load the portal gun455 shoot Shoot the portal gun456+ load Load the portal gun457 ```458 459 </div>460 2 unmodified lines463 Notice that you have to pass the importable version of the package name, so `rick_portal_gun` instead of `rick-portal-gun`.464 465 ///466 472-That works! 🚀 Sort of... 🤔467+That works! 🚀468 474-See the `__main__.py` in the help instead of `rick-portal-gun`? We'll fix that next.475-476-### Set a program name in `__main__.py`477-478-We are setting the program name in the file `pyproject.toml` in the line like:479-480-```TOML481-[tool.poetry.scripts]482-rick-portal-gun = "rick_portal_gun.main:app"483-```484-485-But when Python runs our package as a script with `python -m`, it doesn't have the information of the program name.486-487-So, 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`:488-489-```Python490-from .main import app491-app(prog_name="rick-portal-gun")492-```493-494-/// tip495-496-You can pass all the arguments and keyword arguments you could pass to a Click application, including `prog_name`.497-498-///499-500-<div class="termy">501-502-```console503-$ python -m rick_portal_gun --help504-505-Usage: rick-portal-gun [OPTIONS] COMMAND [ARGS]...506-507- Awesome Portal Gun508-509-Options: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.512-513- --help Show this message and exit.514-515-Commands:516- load Load the portal gun517- shoot Shoot the portal gun518-```519-520-</div>521-522-Great! That works correctly! 🎉 ✅523-524-Notice that now it uses `rick-portal-gun` instead of `__main__.py` in the help.525-469 ### Autocompletion and `python -m`470 471 Have in mind that TAB completion (shell auto-completion) won't work when using `python -m`.472 30 unmodified lines503 ```504 pypi-wubalubadubdub-deadbeef1234505 ```506 564-Now configure Poetry to use this token with the command `poetry config pypi-token.pypi`:507+Now configure uv to use this token by setting an environment variable:508 509 <div class="termy">510 511 ```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 ```515 516 </div>517 518 ### Publish to PyPI519 577-Now you can publish your package with Poetry.520+Now you can publish your package.521 579-You 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:580-522 <div class="termy">523 524 ```console584-$ poetry publish --build525+$ uv publish526 586-# There are 2 files ready for publishing. Build anyway? (yes/no) [no] $ yes587-588----> 100%589-590-Building 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.whl595-596-Publishing 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%527+Publishing 2 files https://upload.pypi.org/legacy/528+Uploading rick_portal_gun-0.1.0-py3-none-any.whl (2.3KiB)529+Uploading rick_portal_gun-0.1.0.tar.gz (841.0B)530 ```531 532 </div>533 2 unmodified lines536 You should now see your new "rick-portal-gun" package.537 538 ### Install from PyPI539 609-Now to see that we can install it form PyPI, open another terminal, and uninstall the currently installed package.540+Now to see that we can install it from PyPI, open another terminal, and uninstall the currently installed package.541 542 <div class="termy">543 544 ```console79 unmodified lines624 625 For that you need to first increase the version in `pyproject.toml`:626 627 ```TOML hl_lines="3"697-[tool.poetry]628+[project]629 name = "rick-portal-gun"630 version = "0.2.0"700-description = ""701-authors = ["Rick Sanchez <rick@example.com>"]631+description = "Add your description here"632 readme = "README.md"633+authors = ["Rick Sanchez <rick@example.com>"]634+requires-python = ">=3.14"635+dependencies = [636+ "typer>=0.21.0",637+]638 704-[tool.poetry.scripts]639+[project.scripts]640 rick-portal-gun = "rick_portal_gun.main:app"641 707-[tool.poetry.dependencies]708-python = "^3.10"709-typer = "^0.12.0"710-642 [build-system]712-requires = ["poetry-core"]713-build-backend = "poetry.core.masonry.api"643+requires = ["uv_build>=0.8.14,<0.9.0"]644+build-backend = "uv_build"645 ```646 716-And in the file `rick_portal_gun/__init__.py`:717-718-```Python719-__version__ = '0.2.0'720-```721-647 And then build and publish again:648 649 <div class="termy">650 651 ```console727-$ poetry publish --build652+$ uv build653+$ uv publish654 729----> 100%730-731-Building 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.whl736-737-Publishing 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%655+Publishing 2 files https://upload.pypi.org/legacy/656+Uploading rick_portal_gun-0.2.0-py3-none-any.whl (2.3KiB)657+Uploading rick_portal_gun-0.2.0.tar.gz (840.0B)658 ```659 660 </div>661 4 unmodified lines666 This is a very simple guide. You could add many more steps.667 668 For 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.669 752-You 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>.670+You 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.671 754-You 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.672+Maybe use automatic formatting with <a href="https://docs.astral.sh/ruff/" class="external-link" target="_blank">Ruff</a>.673 756-Maybe use automatic formatting with <a href="https://github.com/psf/black" class="external-link" target="_blank">Black</a>.757-674 You'll probably want to publish your code as open source to <a href="https://github.com/" class="external-link" target="_blank">GitHub</a>.675 676 And then you could integrate a <abbr title="Continuous Integration">CI</abbr> tool to run your tests and deploy your package automatically.677 762-And there's a long etc. But now you have the basics and you can continue on your own 🚀.678+And there's a long etc. But now you have the basics and you can continue on your own. 🚀