.python-version file - specifies the python version used by the project. Python will be installed if not already installed in the OS
uv.lock file - contains the exact resolved versions that are installed in the project environment. This ensures reproducible virtual environments in all the machines
README.md file - a README file for project documentation
.gitignore file - specifies files to be ignored by git
Managing packages in uv
uv uses vitual environment in the project folder (.venv folder) to manage python packages for the project.
New packages can be added using uv add command. For example, uv add pandas requests will add pandas and requests packages to the project
packages can be removed using uv remove command. For example, uv remove requests will remove requests package from the project
uv add -r requirements.txt command 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 add and uv remove commands will also update the uv.lock and pyproject.toml files
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.toml file 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.toml file
After updating pyproject.toml file, uv sync command should be run to update the virtual environment and uv.lock file
Upgrade packages
Packages can be upgraded using uv lock --upgrade and then running uv sync to update the virtual environment as per the new uv.lock file
uv lock --upgrade works only if the dependencies in the pyproject.toml are mentioned something like pandas>=2.3.1. If a strict dependency version is mentioned (like pandas==2.3.1), upgrade will not be possible.
Comments
Post a Comment