Skip to content

Create a Python Project Using uv and Manage Dependencies

Create a Python Project

uv init pyprojects

This command automatically creates the following project structure inside the pyprojects folder:

1
2
3
4
5
6
.git
.gitignore
.python-version
main.py
pyproject.toml
README.md

pyproject.toml Content

1
2
3
4
5
6
7
[project]
name = "pyprojects"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = []

Run the Entry Script

uv run main.py

Install Dependencies

Add the latest requests package to pyproject.toml:

uv add requests

This updates the file as follows:

1
2
3
dependencies = [
    "requests>=2.32.5",
]

Upgrade a package:

uv add --upgrade requests

Remove a package:

uv remove requests

Add Development Dependencies

uv add --dev pytest

This creates a [dependency-groups] section in pyproject.toml:

1
2
3
4
[dependency-groups]
dev = [
    "pytest>=8.4.2",
]

Add Dependencies from requirements.txt

uv add -r requirements.txt

List Packages in the Virtual Environment

uv pip list

Lock and Sync

Generate a lock file:

uv lock

Synchronize the environment with the lock file:

uv sync