Sub topic 3
Starting a project with uv init
This chapter covers what uv init creates, how the three project shapes differ, and what changes after the first uv run. It assumes you know basic pyproject.toml syntax from the earlier chapter on the [project] table, can use a terminal, and have a general idea of what virtual environments do. It also assumes uv is installed.
What uv init does
uv init scaffolds a project. It writes files and does not install anything. It creates no virtual environment and no lockfile. Those appear later, when you first run uv run, uv sync, uv lock, or uv add.
You can use it in two ways:
uv init weather-clicreates a new directory calledweather-cliand fills it.uv initwith no name fills the current directory. The project name defaults to the directory name. Use--nameto override it.
If the target is not already inside a Git repository, uv also runs git init and writes a .gitignore. Pass --vcs none to skip that.
Choosing a project shape
uv offers three templates. The choice decides whether your code is importable as a package and whether the project has a build system.
| Flag | Layout | Build system | Use it for |
|---|---|---|---|
| `--app` (default) | `main.py` at the root | None | Scripts and services you run in place |
| `--package` | `src/<name>/__init__.py` | Yes, plus a `[project.scripts]` entry | Applications you want to install or distribute as a command |
| `--lib` | `src/<name>/__init__.py` and `py.typed` | Yes | Libraries that other projects import |
The practical difference is the build system. A plain app has no [build-system] table, so uv treats it as a "virtual" project. It installs the dependencies and does not install your own code into the environment. A package or library has a build backend, so uv builds it and installs it into .venv in editable mode. That is why import weather_cli works from anywhere inside the environment, and why console scripts become available.
Running the walkthrough
This is the sequence from the chapter plan. It creates a packaged project, pins Python, adds dependencies, and runs the tests.
uv init --package weather-cli
cd weather-cli
uv python pin 3.12
uv add httpx typer
uv add --dev pytest
uv run pytestThe sections below take the first two commands in turn. The other commands get their own chapters.
The files you get
For uv init --package weather-cli, expect this tree:
weather-cli/
├── .git/
├── .gitignore
├── .python-version
├── README.md
├── pyproject.toml
└── src/
└── weather_cli/
└── __init__.pyNote that the directory is weather-cli and the importable module is weather_cli. Hyphens are legal in distribution names but not in Python identifiers, so uv converts them to underscores for the module.
The .python-version file holds one line, such as 3.12. uv reads it when it picks an interpreter. uv python pin 3.12 rewrites it.
The generated pyproject.toml
The exact content varies with the uv version, especially the build backend and the requires-python value, which defaults to your current interpreter. A generated file looks like this. This copy uses hatchling, which you can request with --build-backend hatch. Newer uv releases default to uv's own uv_build backend.
[project]
name = "weather-cli"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = []
[project.scripts]
weather-cli = "weather_cli:main"
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"The [project.scripts] line is the console script entry point. It says that a command named weather-cli should call main from the weather_cli module. The next chapters change this name to weather and add dependencies.
Two defaults deserve a second look:
dependencies = []is a placeholder.uv addedits this list for you.descriptionis filler text. Replace it before you publish anything.
The starter module
The generated src/weather_cli/__init__.py prints a greeting. To match the chapter plan, replace it with this version:
import typer
def main() -> None:
typer.echo("Sunny")
if __name__ == "__main__":
typer.run(main)This imports typer, so run uv add typer first. Then uv run weather-cli fails if the script name in pyproject.toml and the command you type do not match. Rename the script entry to weather = "weather_cli:main" and run uv run weather.
Useful flags
You rarely need more than the template flag, but these cover most special cases:
--python 3.12records a Python request in.python-versionand setsrequires-pythonto match.--libor--packagewith--build-backend hatchselects hatchling instead of the default backend.--no-readmeskipsREADME.md. Thereadmekey is then omitted frompyproject.toml.--barecreates onlypyproject.toml. It writes no sample code, no README, no.python-version, and no Git repository. Use it when you are adding uv to a directory that already has code.--scriptcreates a single file with inline metadata instead of a project. A later chapter covers this.
What happens on first run
Before your first command, the project has no environment. When you run uv run pytest or uv sync, uv does three things, in this order:
- It finds or downloads a Python that satisfies
.python-versionandrequires-python. - It resolves your dependencies and writes
uv.lock. - It creates
.venvand installs the locked packages into it.
Commit uv.lock, pyproject.toml, and .python-version. The generated .gitignore already excludes .venv, so the environment is not committed.
Common mistakes
- Running
uv initin a directory that already has apyproject.toml. uv stops with an error and does not overwrite the file. Edit the existing file instead. - Expecting an app to be importable. A plain
uv initproject has no build system, soimportof your own code works only through relative paths. Use--packageif you need installation. - Using a project name that is not a valid package name. Names with spaces or leading digits cause errors. Choose lowercase letters, digits, and hyphens.
- Forgetting the workspace side effect. If you run
uv initinside a directory tree that has a parent project with a[tool.uv.workspace]table, uv adds the new project as a workspace member. Pass--no-workspaceto prevent this.
Exercise
You run uv init with no flags, then add import mytool to a test file, where mytool is a module you wrote in main.py's directory. Later you need uv run mytool to work as a command. Which flag should you have used, and which table in pyproject.toml defines the command?
Show answer
Use --package. It creates the src/<name> layout and a build system, so uv installs the project into .venv. The command comes from the [project.scripts] table, which maps a command name to module:function. A plain app has neither a build system nor a scripts table.
Exercise
What files does uv init --bare create, and why is it useful for an existing project?
Show answer
It creates only pyproject.toml. No sample code, README, .python-version, or Git repository is written. That makes it safe to run in a directory that already has code, because nothing you wrote is touched.