Managing Environment with UV
This is a note I wrote for my job about managing python environment with UV.
What is UV?
UV is just like poetry, but with powerful management on python version.
Comparison with poetry: https://blog.kyomind.tw/introducing-uv/.
Installing Trio‘s dependencies with a warm cache.
Installation
1 | # Installing via pip is possible but not recommended. |
Basic Operations
1 | # Init a new project (this will create a folder name xxx) |
You can run your script using
1 | # Run script |
Or you can enter the virtual environment first
1 | # Activate the venv |
Python Version Control
1 | # List available python versions |
Run script fast using —with and cache
1 | # Run py with given packages |
Migrate from Poetry / pip
- Ref: https://github.com/mkniewallner/migrate-to-uv
- Ref: https://stackoverflow.com/questions/79118841/how-can-i-migrate-from-poetry-to-uv-package-manager
With existing poetrypyproject.toml or pip requirements.txt, just run the following command for migration.
1 | # With uv |
You may need to run uv lock afterward if there are not uv.lock. Run uv sync if you want to sync the environment from lock file into your local .venv.
Using UV on Pytorch
Take torch==2.4.0 with CUDA 12.4 as example:
1 | # With given index |
Or add this block in pyproject.toml:
1 | [[tool.uv.index]] |
Using Jupyter Notebook in VSCode
1 | uv add ipykernel |
本部落格所有文章除特別聲明外,均採用CC BY-NC-SA 4.0 授權協議。轉載請註明來源 Jack's Space!
評論









