uv for all-in-one python project management
What is a uv
- uv is an all-in-one project management tool for python projects
- It manages python versions, virtual environments, package dependencies automatically for a python project
Install uv
- uv can be installed in windows using the following command
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
- uv can be installed in linux environments using the following command
curl -LsSf https://astral.sh/uv/install.sh | sh
- uv can also be installed using pipx or pip using the following commands
pipx install uv
Update uv
- uv can be updated using the following command
uv self upgrade
- If uv is installed with pipx, it can be updated with the following command
pipx upgrade uv
Initialize a new project in a folder
- Run the following to initialize a new uv project inside a folder
uv init
uv sync
- This will create
pyproject.tomlfile - contains project metadata like package versions, name, description, readme file location.python-versionfile - specifies the python version used by the project. Python will be installed if not already installed in the OSuv.lockfile - contains the exact resolved versions that are installed in the project environment. This ensures reproducible virtual environments in all the machinesREADME.mdfile - a README file for project documentation.gitignorefile - specifies files to be ignored by git
Managing packages in uv
- uv uses vitual environment in the project folder (
.venvfolder) to manage python packages for the project. - New packages can be added using
uv addcommand. For example,uv add pandas requestswill add pandas and requests packages to the project - packages can be removed using
uv removecommand. For example,uv remove requestswill remove requests package from the project uv add -r requirements.txtcommand will add packages mentioned in requirements.txt file into the project. This command is especially to migrate conventional virtual environment based project to a uv project.uv addanduv removecommands will also update theuv.lockandpyproject.tomlfiles
Running uv project
- Run entrypoint python script of the uv project using the following command
uv run main.py
- This command will use the virtual environment and run the python script
Changing packages in pyproject.toml
- A sample
pyproject.tomlfile can be as follows
[project]
name = "python-command-runner"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = [
"flask>=3.1.1",
"flask-socketio>=5.5.1",
"python-socketio[client]>=5.13.0",
]
- Adddition, deletion, modification of python dependencies can be done in the dependencies variable of the project section of the
pyproject.tomlfile - After updating
pyproject.tomlfile,uv synccommand should be run to update the virtual environment and uv.lock file
Upgrade packages
- Packages can be upgraded using
uv lock --upgradeand then runninguv syncto update the virtual environment as per the newuv.lockfile uv lock --upgradeworks only if the dependencies in thepyproject.tomlare mentioned something likepandas>=2.3.1. If a strict dependency version is mentioned (like pandas==2.3.1), upgrade will not be possible.
Video
Video can be found here
References
- uv GitHub repository - https://github.com/astral-sh/uv
- uv official documentation - https://docs.astral.sh/uv/
Comments
Post a Comment