Saltar al contingut

UV

uv és un gestor de paquets i projectes Python d'última generació construït amb Rust

uv proporciona característiques essencials per al desenvolupament en Python — des de la instal·lació de Python i la programació de scripts senzills fins al treball en grans projectes que admeten múltiples versions de Python i plataformes.

Instal·la uv amb Scoop:

Terminal window
scoop install uv

La interfície de uv es pot dividir en seccions, que es poden utilitzar de manera independent o conjunta.

Si Python ja està instal·lat al teu sistema, uv el detectarà i l’utilitzarà sense configuració. No obstant això, uv també pot instal·lar i gestionar versions de Python. uv instal·la automàticament les versions de Python que falten segons sigui necessari — no cal instal·lar Python per començar.

uv python installInstal·la versions de Python
uv python listMostra les versions de Python disponibles
uv python findTroba una versió de Python instal·lada
uv python pinFixa el projecte actual per utilitzar una versió específica de Python
uv python uninstallDesinstal·la una versió de Python

Installing Python

Per instal·lar l’última versió de Python:

Terminal window
uv python install

Un cop Python està instal·lat, serà utilitzat automàticament per les comandes de uv.

To install a specific Python version:

Terminal window
uv python install 3.12

To install multiple Python versions:

Terminal window
uv python install 3.11 3.12

To install an alternative Python implementation, e.g., PyPy:

Terminal window
uv python install pypy@3.10

See the python install documentation for more details.

To reinstall uv-managed Python versions, use --reinstall, e.g.:

Terminal window
uv python install --reinstall

This will reinstall all previously installed Python versions. Improvements are constantly being added to the Python distributions, so reinstalling may resolve bugs even if the Python version does not change.

To view available and installed Python versions:

Terminal window
uv python list

See the python list documentation for more details.

A Python script is a file intended for standalone execution, e.g., with python <script>.py. Using uv to execute scripts ensures that script dependencies are managed without manually managing environments.

If your script has no dependencies, you can execute it with uv run:

example.py
print("Hello world")
Terminal window
uv run example.py

Similarly, if your script depends on a module in the standard library, there’s nothing more to do:

example.py
import os
print(os.path.expanduser("~"))
Terminal window
uv run example.py

Arguments may be provided to the script:

example.py
import sys
print(" ".join(sys.argv[1:]))
Terminal window
uv run example.py test
uv run example.py hello world!

uv -scripts

uv supports managing Python projects, which define their dependencies in a pyproject.toml file.

You can create a new Python project using the uv init command:

Terminal window
uv init hello
cd hello

Alternatively, you can initialize a project in the working directory:

Terminal window
mkdir hello
cd hello
uv init

uv will create the following files:

Terminal window
├── .gitignore
├── .python-version
├── README.md
├── main.py
└── pyproject.toml

The main.py file contains a simple “Hello world” program. Try it out with uv run:

Terminal window
uv run main.py

A project consists of a few important parts that work together and allow uv to manage your project.

In addition to the files created by uv init, uv will create a virtual environment and uv.lock file in the root of your project the first time you run a project command, i.e., uv run, uv sync, or uv lock.

You can add dependencies to your pyproject.toml with the uv add command.

This will also update the lockfile and project environment:

Terminal window
uv add polars

El contingut d'aquest lloc web té llicència CC BY-NC-ND 4.0.

©2022-2025 xtec.dev