2025-01-16 04:12:55 +08:00
---
title: Working on projects
description:
A guide to using uv to create and manage Python projects, including adding dependencies, running
commands, and building publishable distributions.
---
2024-07-16 18:02:54 -04:00
# Working on projects
2024-08-27 16:22:31 -05:00
uv supports managing Python projects, which define their dependencies in a `pyproject.toml` file.
2024-07-18 20:45:25 -04:00
2024-07-16 18:02:54 -04:00
## Creating a new project
2024-07-18 20:45:25 -04:00
You can create a new Python project using the `uv init` command:
``` console
$ uv init hello-world
$ cd hello-world
```
Alternatively, you can initialize a project in the working directory:
``` console
2024-07-22 13:15:11 -04:00
$ mkdir hello-world
2024-07-18 20:45:25 -04:00
$ cd hello-world
$ uv init
```
2024-08-27 16:22:31 -05:00
uv will create the following files:
2024-07-18 20:45:25 -04:00
2024-07-23 14:29:59 -04:00
``` text
2024-07-18 20:45:25 -04:00
.
2024-09-05 00:22:34 +01:00
├── .python-version
2024-07-18 20:45:25 -04:00
├── README.md
2025-02-13 16:58:02 -05:00
├── main.py
2024-08-27 16:22:31 -05:00
└── pyproject.toml
2024-07-16 18:02:54 -04:00
```
2025-02-13 16:58:02 -05:00
The `main.py` file contains a simple "Hello world" program. Try it out with `uv run` :
2024-07-18 20:45:25 -04:00
2024-08-27 16:22:31 -05:00
``` console
2025-02-13 16:58:02 -05:00
$ uv run main.py
2024-08-27 16:22:31 -05:00
Hello from hello-world!
```
2024-07-18 20:45:25 -04:00
## Project structure
2024-07-30 18:17:58 -04:00
A project consists of a few important parts that work together and allow uv to manage your project.
2024-08-27 16:22:31 -05:00
In addition to the files created by `uv init` , uv will create a virtual environment and `uv.lock`
file in the root of your project the first time you run a project command, i.e., `uv run` ,
`uv sync` , or `uv lock` .
A complete listing would look like:
``` text
.
├── .venv
│ ├── bin
│ ├── lib
│ └── pyvenv.cfg
2024-09-05 00:22:34 +01:00
├── .python-version
2024-08-27 16:22:31 -05:00
├── README.md
2025-02-13 16:58:02 -05:00
├── main.py
2024-08-27 16:22:31 -05:00
├── pyproject.toml
└── uv.lock
```
2024-07-18 20:45:25 -04:00
### `pyproject.toml`
The `pyproject.toml` contains metadata about your project:
2024-07-23 14:29:59 -04:00
```toml title="pyproject.toml"
2024-07-18 20:45:25 -04:00
[project]
name = "hello-world"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
dependencies = []
` ``
2024-08-27 16:22:31 -05:00
You'll use this file to specify dependencies, as well as details about the project such as its
description or license. You can edit this file manually, or use commands like ` uv add` and
` uv remove` to manage your project from the terminal.
2024-07-18 20:45:25 -04:00
2024-07-24 18:10:33 -04:00
!!! tip
2024-08-02 15:58:31 +02:00
See the official [` pyproject.toml` guide](https://packaging.python.org/en/latest/guides/writing-pyproject-toml/)
2024-07-30 19:26:23 -04:00
for more details on getting started with the ` pyproject.toml` format.
2024-07-24 18:10:33 -04:00
2024-08-27 16:22:31 -05:00
You'll also use this file to specify uv [configuration options](../configuration/files.md) in a
[` [tool.uv]`](../reference/settings.md) section.
2024-09-05 00:22:34 +01:00
### ` .python-version`
The ` .python-version` file contains the project's default Python version. This file tells uv which
Python version to use when creating the project's virtual environment.
2024-07-18 20:45:25 -04:00
### ` .venv`
2024-07-30 18:17:58 -04:00
The ` .venv` folder contains your project's virtual environment, a Python environment that is
isolated from the rest of your system. This is where uv will install your project's dependencies.
2024-07-16 18:02:54 -04:00
2024-11-19 13:52:12 -06:00
See the [project environment](../concepts/projects/layout.md#the-project-environment) documentation
for more details.
2024-07-30 19:26:23 -04:00
2024-07-18 20:45:25 -04:00
### ` uv.lock`
2024-07-30 18:17:58 -04:00
` uv.lock` is a cross-platform lockfile that contains exact information about your project's
dependencies. Unlike the ` pyproject.toml` which is used to specify the broad requirements of your
2024-07-30 19:26:23 -04:00
project, the lockfile contains the exact resolved versions that are installed in the project
2024-07-30 18:17:58 -04:00
environment. This file should be checked into version control, allowing for consistent and
reproducible installations across machines.
2024-07-18 20:45:25 -04:00
2024-07-30 18:17:58 -04:00
` uv.lock` is a human-readable TOML file but is managed by uv and should not be edited manually.
2024-07-16 18:02:54 -04:00
2024-11-19 13:52:12 -06:00
See the [lockfile](../concepts/projects/layout.md#the-lockfile) documentation for more details.
2024-07-24 18:10:33 -04:00
2024-07-16 18:02:54 -04:00
## Managing dependencies
2024-07-30 18:17:58 -04:00
You can add dependencies to your ` pyproject.toml` with the ` uv add` command. This will also update
2024-07-30 19:26:23 -04:00
the lockfile and project environment:
2024-07-18 20:45:25 -04:00
` ``console
$ uv add requests
2024-07-16 18:02:54 -04:00
` ``
2024-07-18 20:45:25 -04:00
You can also specify version constraints or alternative sources:
` ``console
2024-08-03 08:41:33 -05:00
$ # Specify a version constraint
2024-07-18 20:45:25 -04:00
$ uv add 'requests==2.31.0'
2024-08-03 08:41:33 -05:00
$ # Add a git dependency
2024-10-04 12:42:18 +02:00
$ uv add git+https://github.com/psf/requests
2024-07-16 18:02:54 -04:00
` ``
2024-07-18 20:45:25 -04:00
To remove a package, you can use ` uv remove`:
` ``console
$ uv remove requests
2024-07-16 18:02:54 -04:00
` ``
2024-09-25 18:17:55 -04:00
To upgrade a package, run ` uv lock` with the ` --upgrade-package` flag:
` ``console
$ uv lock --upgrade-package requests
` ``
The ` --upgrade-package` flag will attempt to update the specified package to the latest compatible
version, while keeping the rest of the lockfile intact.
2024-11-19 13:52:12 -06:00
See the documentation on [managing dependencies](../concepts/projects/dependencies.md) for more
details.
2024-07-30 19:26:23 -04:00
## Running commands
2024-08-05 09:54:06 -04:00
` uv run` can be used to run arbitrary scripts or commands in your project environment.
Prior to every ` uv run` invocation, uv will verify that the lockfile is up-to-date with the
` pyproject.toml`, and that the environment is up-to-date with the lockfile, keeping your project
in-sync without the need for manual intervention. ` uv run` guarantees that your command is run in a
consistent, locked environment.
2024-07-30 19:26:23 -04:00
For example, to use ` flask`:
` ``console
$ uv add flask
$ uv run -- flask run -p 3000
` ``
Or, to run a script:
` ``python title="example.py"
# Require a project dependency
import flask
print("hello world")
` ``
` ``console
$ uv run example.py
` ``
Alternatively, you can use ` uv sync` to manually update the environment then activate it before
executing a command:
2025-01-09 16:43:09 -05:00
=== "macOS and Linux"
` ``console
$ uv sync
$ source .venv/bin/activate
$ flask run -p 3000
$ python example.py
` ``
=== "Windows"
` ``powershell
uv sync
source .venv\Scripts\activate
flask run -p 3000
python example.py
` ``
2024-07-30 19:26:23 -04:00
!!! note
The virtual environment must be active to run scripts and commands in the project without ` uv run`. Virtual environment activation differs per shell and platform.
2024-11-19 13:52:12 -06:00
See the documentation on [running commands and scripts](../concepts/projects/run.md) in projects for
more details.
2024-07-30 19:26:23 -04:00
2024-09-04 11:53:14 -04:00
## Building distributions
` uv build` can be used to build source distributions and binary distributions (wheel) for your
project.
By default, ` uv build` will build the project in the current directory, and place the built
artifacts in a ` dist/` subdirectory:
` ``console
$ uv build
$ ls dist/
hello-world-0.1.0-py3-none-any.whl
hello-world-0.1.0.tar.gz
` ``
2024-11-19 13:52:12 -06:00
See the documentation on [building projects ](../concepts/projects/build.md ) for more details.
2024-09-04 11:53:14 -04:00
2024-07-16 18:02:54 -04:00
## Next steps
2024-11-19 13:52:12 -06:00
To learn more about working on projects with uv, see the
[projects concept ](../concepts/projects/index.md ) page and the
[command reference ](../reference/cli.md#uv ).
2024-08-03 08:41:33 -05:00
2025-02-05 17:22:09 -06:00
Or, read on to learn how to [build and publish your project to a package index ](./package.md ).