@@ -4,6 +4,12 @@ The first choice you have to face setting up Python is which distribution you
44want: official packaging using PyPA tools (like pip), or the conda Python and
55packages. Both options will be outlined below.
66
7+ There are also modern Rust-based alternatives; uv replaces the PyPA tooling, and
8+ pixi replaces the conda/mamba family. Unless you have a reason not to use them,
9+ you should probably start with them, as they are simple, modern, and many times
10+ faster than the classic tools. After describing the classic tooling below,
11+ you'll find information on these new alternatives.
12+
713## PyPA packaging
814
915To use the standard tools, you can get Python from anywhere (except Conda, see
@@ -14,13 +20,14 @@ already using it to manage everything else on your system.
1420
1521On Windows, typing ` python ` will open the Windows store if it's not installed.
1622You can also use ` winget install Python ` , Window's official package manager on
17- recent versions, to get Python from the Windows store.
23+ recent versions, to get Python from the Windows store. (If you are using WSL, it
24+ behaves like Linux below.)
1825
1926For Linux, if you use your system Python, make sure it is new enough, and never
2027modify the base environment except though your package manager (generally true,
2128but more so here). The system Python is really intended for use in other system
2229packages, and is not intended for you to modify. Modern pip and modern systems
23- now work together to provide safegaurds for this.
30+ (like Ubuntu 24.04+) now work together to provide safegaurds for this.
2431
2532### Virtual environments
2633
@@ -168,37 +175,37 @@ interesting entry, so let's cover that below.
168175#### uv
169176
170177The team at astral-sh has been developing Rust-based tooling for Python. They
171- recently introduced ` uv ` , which started out as a drop-in replacement for venv,
172- pip , and pip-tools that was 10-100x faster. They also have a more modern design
173- since they don't have to worry about backwards-compatibly (so technically not
174- quite drop-in), and had many long-requested features added (to be fair, uv has
175- likely had more dedicated developer time than these other tools, probably
176- combined). Since launching, they've also replaced pipx, build, Python
177- installers, and are starting to replace poetry/pdm. By targeting the stand alone
178- tools first, it's easy to just use uv for whatever you want faster without fully
179- committing to it like, for example, Poetry forces you to do.
178+ introduced ` uv ` , which started out as a drop-in replacement for venv, quite
179+ drop-in) , and had many long-requested features added (to be fair, uv has has
180+ more dedicated developer time than these other tools combined). Since launching,
181+ they've also replaced pipx, build, Python installers, and are starting to
182+ replace poetry/pdm. By targeting the stand alone tools first, it's easy to just
183+ use uv for whatever you want faster without fully committing to it like, for
184+ example, Poetry forces you to do. There's also a high-level interface.
185+
186+ ##### uv: low level interface
180187
181188If you use ` uv venv ` , this creates virtual environments faster than Python can
182189start up. They do not, by default, contain _ anything_ , since uv was designed to
183190be able to target a virtual environment from the outside (modern pip can too,
184191but for legacy reasons, we are used to running it from inside the virtual
185- environment).
192+ environment). It also defaults to the folder ` .venv ` if unspecified.
186193
187194If you use ` uv pip install ` , you will get an ultra-fast package installer. A few
188- key differences: It will look for a virtual environment named ` ./. venv ` if one
189- is not active by default, it will not install to the system Python unless you
190- add ` --system ` or pass an path to ` --python ` , and it will never install to the
191- user location. It also has some amazing features, like limiting the date of the
192- searched packages, and a minimum-versions resolver so you can make sure your
193- stated minimums are valid.
195+ key differences: It will look for a virtual environment named ` .venv ` if one is
196+ not active by default, it will not install to the system Python unless you add
197+ ` --system ` or pass an path to ` --python ` , and it will never install to the user
198+ location. It also has some amazing features that are not present in pip, like
199+ limiting the date of the searched packages, and a minimum-versions resolver so
200+ you can make sure your stated minimums are valid.
194201
195202If you use ` uv pip compile ` , you'll get an ultra-fast lock file generator that
196203can target versions of Python and platforms you don't even have.
197204
198- If you use ` uv tool run ` (or ` uvx ` ), you'll get a tool runner that can run any
199- Python tool in a temporary virtual environment. You can also use
200- ` uv tool install ` to manage tools. And you can use ` uv run ` to run a script with
201- dependencies.
205+ If you use ` uv tool run ` (or ` uvx ` , which is an equivalent shortcut), you'll get
206+ a tool runner that can run any Python tool in a temporary virtual environment.
207+ You can also use ` uv tool install ` to manage tools. And you can use ` uv run ` to
208+ run a script with dependencies.
202209
203210If you need to build packages, ` uv build ` is a drop-in replacement for the
204211standard pypa/build.
@@ -208,17 +215,103 @@ commands will install Python on your system for you in a uv managed location.
208215After installing, uv commands will prefer these managed versions. These are
209216binary installs, so they are faster than most of the classic tools like pyenv.
210217
218+ ##### uv: high level interface (setup)
219+
211220The most recent addition is a series of poetry/pdm-like project commands, such
212221as ` uv init ` , ` uv add ` , and ` uv sync ` . These let you set up a managed
213222environment with an integrated lockfile. There is also now uv configuration in
214- ` pyproject.toml ` or ` uv.toml ` .
223+ ` pyproject.toml ` or ` uv.toml ` . The highest level command is ` uv run ` .
224+
225+ To use the high level interface, you need a ` pyproject.toml ` that contains a few
226+ of the standard package configuration lines. Here's an example showing the
227+ various things uv finds useful. Remember, you can get started with ` uv init ` ,
228+ which will write this file for you:
229+
230+ ``` toml
231+ [build-system ]
232+ requires = [" uv_build" ]
233+ build-backend = " uv_build"
234+
235+ [project ]
236+ name = " example"
237+ version = " 0.0.1"
238+ requires-python = " >=3.11"
239+ dependencies = []
240+ ```
241+
242+ The ` build-system ` is highly recommended, as it will allow this file to work
243+ with other tools properly too, though if it's not a library and you are never
244+ using anything besides uv and you aren't going to make SDists/wheels, you can
245+ leave it off. ` "hatchling" ` is a great, flexible, and extendable backend, but
246+ there is a built in backend as well (shown above) for very simple projects. If
247+ you use hatchling, make sure the import name matches the project name (or look
248+ up it's configuration options if you have a good reason not to match the names,
249+ which you don't).
250+
251+ The ` [project] ` table should always have ` name ` and ` version ` , as they are
252+ required because they will be part of the filename in several places, including
253+ intermediate installation files. You don't have to have ` requires-python ` , but
254+ since uv is a universal solver (meaning it solves for all Python versions
255+ supported, rather than just the one you are on), it will affect the solve, and
256+ you'll get a warning if it's missing.
257+
258+ Then the ` dependencies ` list contains every library your code needs to run.
259+ Things like ` "numpy" ` will likely be here. If it's empty, you don't have to have
260+ it, but most projects will have at least a few dependencies.
261+
262+ There's also a way to specify development dependencies that aren't required to
263+ use your package, but are required for developing your package. For example, if
264+ you use ` pytest ` for tests, you might do this:
265+
266+ ``` toml
267+ [dependency-groups ]
268+ dev = [" pytest" ]
269+ ```
270+
271+ ` uv ` will automatically install the ` dev ` group with it's high level commands.
272+ If you'd like to break it up further, you can:
273+
274+ ``` toml
275+ [dependency-groups ]
276+ test = [" pytest" ]
277+ dev = [{ include-group = " test" }]
278+ ```
279+
280+ Now you can have separate groups for tests, documentation, and other things, and
281+ combine the ones that are useful for ` uv run ` using ` include-group ` into the
282+ ` dev ` group.
283+
284+ Task support and hopefully multiple environments are coming eventually, which
285+ would basically allow uv to be a true all-in-one tool for Python development.
286+
287+ ##### uv: high level interface (usage)
288+
289+ Once you've setup a package, or if you are using someone else's package that has
290+ already been setup, then using it is simple; just prefix anything you want to
291+ run with ` uv run ` . For example:
292+
293+ ``` bash
294+ uv run python
295+ ```
296+
297+ This will create a ` .venv ` if it doesn't exist, create a lockfile if it doesn't
298+ exist, or read an existing one if it does, do an editable install your package
299+ if not installed, install the ` dev ` group if it exists, and run whatever command
300+ you give it inside the environment.
301+
302+ As another example, you can run pytest:
303+
304+ ``` bash
305+ uv run pytest
306+ ```
307+
308+ The commands are fast if everything is set up from an earlier run.
215309
216- Task support and hopefully multiple environments are coming soon, which would
217- basically allow uv to be a true all-in-one tool for Python development.
310+ ##### uv: other tool support
218311
219- Many of the above tools also support using uv, such as ` nox ` , ` build ` , ` hatch ` ,
220- and ` pdm ` , usually with flags or configuration settings. There is also a plugin
221- for ` tox ` and a hack for ` pre-commit ` .
312+ Many of the standard Python tools also support using uv, such as ` nox ` , ` build ` ,
313+ ` hatch ` , and ` pdm ` , usually with flags or configuration settings. There is also
314+ a plugin for ` tox ` and a hack for ` pre-commit ` .
222315
223316## Conda
224317
0 commit comments