uv és un gestor de paquets i projectes Python d'última generació construït amb Rust
Introducció
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
:
Utilitza irm
per descarregar l’script i executa’l amb iex
:
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
curl -LsSf https://astral.sh/uv/install.sh | sh
source $HOME/.local/bin/env
La interfície de uv es pot dividir en seccions, que es poden utilitzar de manera independent o conjunta.
Python
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 install | Instal·la versions de Python |
uv python list | Mostra les versions de Python disponibles |
uv python find | Troba una versió de Python instal·lada |
uv python pin | Fixa el projecte actual per utilitzar una versió específica de Python |
uv python uninstall | Desinstal·la una versió de Python |
Per instal·lar l’última versió de Python:
uv python install
Un cop Python està instal·lat, serà utilitzat automàticament per les comandes de uv
.
Installing a specific version
To install a specific Python version:
uv python install 3.12
To install multiple Python versions:
uv python install 3.11 3.12
To install an alternative Python implementation, e.g., PyPy:
uv python install pypy@3.10
See the python install documentation for more details.
Reinstalling Python
To reinstall uv-managed Python versions, use --reinstall
, e.g.:
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.
Viewing Python installations
To view available and installed Python versions:
uv python list
See the python list documentation for more details.
Scripts
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.
Running a script without dependencies
If your script has no dependencies, you can execute it with uv run
:
print("Hello world")
uv run example.py
Similarly, if your script depends on a module in the standard library, there’s nothing more to do:
import os
print(os.path.expanduser("~"))
uv run example.py
Arguments may be provided to the script:
import sys
print(" ".join(sys.argv[1:]))
uv run example.py test
uv run example.py hello world!
Project
uv supports managing Python projects, which define their dependencies in a pyproject.toml
file.
Creating a new project
You can create a new Python project using the uv init
command:
uv init hello
cd hello
Alternatively, you can initialize a project in the working directory:
mkdir hello
cd hello
uv init
uv will create the following files:
├── .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
:
uv run main.py
Project structure
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
.
…
Managing dependencies
You can add dependencies to your pyproject.toml
with the uv add
command.
This will also update the lockfile and project environment:
uv add polars
Gestionat l’entorn virtual
Managing Python Virtual Environments with UV: A Comprehensive Guide