Skip to content

Commit d5d8375

Browse files
authored
Update project documentation for the application / library concepts (#6718)
Follows #6689 and #6585
1 parent 2c5cc62 commit d5d8375

File tree

3 files changed

+113
-27
lines changed

3 files changed

+113
-27
lines changed

docs/concepts/projects.md

+71-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ Python projects help manage Python applications spanning multiple files.
1010

1111
Python project metadata is defined in a `pyproject.toml` file.
1212

13-
`uv init` can be used to create a new project, with a basic `pyproject.toml` and package definition.
13+
!!!
14+
15+
`uv init` can be used to create a new project. See [Creating projects](#creating-projects) for
16+
details.
1417

1518
A minimal project definition includes a name, version, and description:
1619

@@ -21,7 +24,7 @@ version = "0.1.0"
2124
description = "Add your description here"
2225
```
2326

24-
Additionally, it's recommended to include a Python version requirement:
27+
It's recommended, but not required, to include a Python version requirement:
2528

2629
```toml title="pyproject.toml"
2730
[project]
@@ -39,6 +42,72 @@ dependency list from the command line with `uv add` and `uv remove`. uv also sup
3942

4043
See the official [`pyproject.toml` guide](https://packaging.python.org/en/latest/guides/writing-pyproject-toml/) for more details on getting started with a `pyproject.toml`.
4144

45+
## Creating projects
46+
47+
uv supports initializing a project with `uv init`. By default, uv will create a project in the
48+
working directory. Projects can be created in a target directory by providing a name, e.g.,
49+
`uv init foo`. If there's already a project in the target directory, i.e., there's a
50+
`pyproject.toml`, uv will exit with an error.
51+
52+
By default, uv will create a project for an [application](#applications). However, the `--lib` flag
53+
can be used to create a project for a [library](#libraries).
54+
55+
## Project types
56+
57+
uv groups projects into two types: **applications** and **libraries**.
58+
59+
### Applications
60+
61+
Application projects are suitable for web servers, scripts, and command-line interfaces.
62+
63+
Application projects have the following traits:
64+
65+
- A build backend is not defined.
66+
- Source code is often in the top-level directory, e.g., `hello.py`.
67+
- The project package is not installed in the project environment.
68+
69+
!!! note
70+
71+
The `--package` flag can be passed to `uv init` to create a distributable application,
72+
e.g., if you want to publish a command-line interface via PyPI. uv will define a build
73+
backend for the project, include a `[project.scripts]` entrypoint, and install the project
74+
package into the project environment.
75+
76+
Similarly, a build backend can be manually defined to treat any application as a distributable
77+
package. This may require changes to the project directory structure, depending on the build
78+
backend.
79+
80+
### Libraries
81+
82+
A library is a project that is intended to be built and distributed as a Python package, for
83+
example, by uploading it to PyPI. A library provides functions and objects for other projects to
84+
consume.
85+
86+
Library projects have the following traits:
87+
88+
- A build backend is required.
89+
- Source code is typically in a `src/{package}` directory.
90+
- The project package is installed in the project environment.
91+
92+
Libraries can be created with `uv init` by providing the `--lib` flag. uv will assume that any
93+
project that defines a `[build-system]` is a library.
94+
95+
### Other types
96+
97+
By default, uv uses the presence of a `[build-system]` in the `pyproject.toml` to determine if a
98+
project is an application or a library. However, uv also allows manually declaring if a project
99+
should be treated as a package.
100+
101+
To enable or disable build and installation of the project package regardless of the presence of a
102+
`[build-system]` definition, use the [`tool.uv.package`](../reference/settings.md#package) setting.
103+
104+
Setting `tool.uv.package = true` will force a project package to be built and installed into the
105+
project environment. If no build system is defined, uv will use the setuptools legacy backend.
106+
107+
Setting `tool.uv.package = false` will force a project package _not_ to be built and installed into
108+
the project environment. uv will ignore the declared build system, if any, when interacting with the
109+
project.
110+
42111
## Project environments
43112

44113
uv creates a virtual environment in a `.venv` directory next to the `pyproject.toml`. This virtual

docs/guides/projects.md

+32-25
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Working on projects
22

3-
uv is capable of managing Python projects using a `pyproject.toml` with a `[project]` metadata
4-
table.
3+
uv supports managing Python projects, which define their dependencies in a `pyproject.toml` file.
54

65
## Creating a new project
76

@@ -20,34 +19,42 @@ $ cd hello-world
2019
$ uv init
2120
```
2221

23-
This will create the following directory structure:
22+
uv will create the following files:
2423

2524
```text
2625
.
27-
├── pyproject.toml
2826
├── README.md
29-
└── src
30-
└── hello_world
31-
└── __init__.py
27+
├── hello.py
28+
└── pyproject.toml
3229
```
3330

34-
### Working on an existing project
35-
36-
If your project already contains a standard `pyproject.toml`, you can start using uv immediately.
37-
Commands like `uv add` and `uv run` will create a [lockfile](#uvlock) and [environment](#venv) the
38-
first time they are used.
31+
The `hello.py` file contains a simple "Hello world" program. Try it out with `uv run`:
3932

40-
If you are migrating from an alternative Python package manager, you may need to edit your
41-
`pyproject.toml` manually before using uv. Most Python package managers extend the `pyproject.toml`
42-
standard to support common features, such as development dependencies. These extensions are specific
43-
to each package manager and will need to be converted to uv's format. See the documentation on
44-
[project dependencies](../concepts/dependencies.md) for more details.
33+
```console
34+
$ uv run hello.py
35+
Hello from hello-world!
36+
```
4537

4638
## Project structure
4739

4840
A project consists of a few important parts that work together and allow uv to manage your project.
49-
Along with the files created by `uv init`, uv will create a virtual environment and `uv.lock` file
50-
in the root of your project the first time you run a project command.
41+
In addition to the files created by `uv init`, uv will create a virtual environment and `uv.lock`
42+
file in the root of your project the first time you run a project command, i.e., `uv run`,
43+
`uv sync`, or `uv lock`.
44+
45+
A complete listing would look like:
46+
47+
```text
48+
.
49+
├── .venv
50+
│   ├── bin
51+
│   ├── lib
52+
│   └── pyvenv.cfg
53+
├── README.md
54+
├── hello.py
55+
├── pyproject.toml
56+
└── uv.lock
57+
```
5158

5259
### `pyproject.toml`
5360

@@ -60,20 +67,20 @@ version = "0.1.0"
6067
description = "Add your description here"
6168
readme = "README.md"
6269
dependencies = []
63-
64-
[tool.uv]
65-
dev-dependencies = []
6670
```
6771

68-
This is where you specify dependencies, as well as details about the project such as its description
69-
or license. You can edit this file manually, or use commands like `uv add` and `uv remove` to manage
70-
your project through the CLI.
72+
You'll use this file to specify dependencies, as well as details about the project such as its
73+
description or license. You can edit this file manually, or use commands like `uv add` and
74+
`uv remove` to manage your project from the terminal.
7175

7276
!!! tip
7377

7478
See the official [`pyproject.toml` guide](https://packaging.python.org/en/latest/guides/writing-pyproject-toml/)
7579
for more details on getting started with the `pyproject.toml` format.
7680

81+
You'll also use this file to specify uv [configuration options](../configuration/files.md) in a
82+
[`[tool.uv]`](../reference/settings.md) section.
83+
7784
### `.venv`
7885

7986
The `.venv` folder contains your project's virtual environment, a Python environment that is

docs/guides/publish.md

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ uv does not yet have dedicated commands for building and publishing a package. I
44
the PyPA tools [`build`](https://github.com/pypa/build) and
55
[`twine`](https://github.com/pypa/twine), both of which can be invoked via `uvx`.
66

7+
## Preparing your project for packaging
8+
9+
Before attempting to publish your project, you'll want to make sure it's ready to be packaged for
10+
distribution.
11+
12+
If your project does not include a `[build-system]` definition in the `pyproject.toml`, uv will not
13+
build it by default. This means that your project may not be ready for distribution. Read more about
14+
the effect of declaring a build system in the
15+
[project concept](../concepts/projects.md#project-types) documentation.
16+
717
## Building your package
818

919
Build your package with the official `build` frontend:

0 commit comments

Comments
 (0)