I just got into using UV, and I needed to onboard a few projects. UV recently changed how it uses virtual environments.
- Create
pyproject.tomlif it does not exist.
uv init
- Create the virtual environment
uv venv
- Activate the virtual environment. This is not needed if
uv venvwas created without overriding the default name. Then, usinguv run ...automatically detects the virtual environment.
source .venv/bin/activate
- Install dependencies
uv sync
- Move dependencies defined in
requirements.txtintopyproject.toml
uv add -r requirements.txt
- Run the script
uv run .\file.py