iTranslated by AI
Migrating from pyenv+venv to uv
The Rivalry of Python Package Management
Just when I thought Poetry was the trend, Rye appeared, and next it's uv?
You people are so fickle! Thinking this, I stubbornly stuck to a setup using pyenv+venv for experimental environments and hard-coding pip install in dockerfile for deployment. However, after trying out uv, I felt it was faster and simpler, so I decided to switch. Below is the minimal replacement. It seems there are many other useful features (or rather, the "correct" ways to use it), but since I probably can't handle them all, please refer to other articles.
Environment
ubuntu 20.04/22.04
Installation
curl -LsSf https://astral.sh/uv/install.sh | sh
uv -h
Creating venv
Where you previously used python -m venv {venv_name}:
uv venv {venv_name(optional)} --python {3.x.y(optional)}
You can activate it the same way with source .venv/bin/activate.
The Python extension in VS Code also correctly recognizes it as an interpreter.
pip install
Where you previously used pip install xxx:
uv pip install xxx
uv pip install -r requirements.txt
Note that within the .venv, the python command uses the Python inside uv, but the pip command will use the external pip.
In other words, please use uv pip instead of pip.
uv add
The features mentioned above are merely simple replacements for pyenv+venv and don't fully leverage the true potential of uv. Using uv pip install only installs into the .venv of that directory, which doesn't ensure project reproducibility.
This is where uv add comes in.
Instead of using pip install xxx, you can use:
uv init
uv add xxx
uv add -r requirements.txt
By doing this, the libraries and versions will be recorded in pyproject.toml (think of it as a richer version of requirements.txt).
For proper usage like this, please refer to the official documentation.
Discussion