Sub topic 3
Starting a project with uv init
This chapter assumes you can run Python from a terminal, know what a package and an import are, and have a rough idea of what pip and virtual environments do. It also assumes uv is installed. The chapter covers what uv init creates, how the flags change that, and the errors people hit most often.
What uv init does
uv init creates a new project: a pyproject.toml file, a starter source file, and a .python-version file. It does not create the virtual environment or the lockfile. Those appear the first time you run uv add, uv sync, or uv run.
You can run it in two ways:
uv init namecreates a new directory calledname.uv initwith no name turns the current directory into a project.
Three project shapes
The flag you choose decides the layout.
| Command | Layout | Build system | Use it for |
|---|---|---|---|
| `uv init name` | `main.py` at the top level | None | Applications and scripts |
| `uv init --package name` | `src/name/__init__.py` | Yes | Command-line tools and anything you distribute |
| `uv init --lib name` | `src/name/__init__.py` and `py.typed` | Yes | Libraries that others import |
Without a build system, uv treats the project as something to run, not something to install. With --package or --lib, uv adds a [build-system] table, and the project itself is installed into its environment. That is what makes imports like import weather_cli work from anywhere in the environment, and what lets you define a console script.
Worked example
uv init --package weather-cli
cd weather-cliThe result looks like this:
weather-cli/
├── .python-version
├── README.md
├── pyproject.toml
└── src/
└── weather_cli/
└── __init__.pyNote that the directory name weather-cli becomes the import name weather_cli. Hyphens are valid in a distribution name but not in a Python identifier, so uv replaces them with underscores for the folder.
The generated pyproject.toml contains a [project] table with the name, a version of 0.1.0, a description, a requires-python bound taken from your current interpreter, and an empty dependencies list. The [build-system] table is included because you passed --package.
Common errors
- A project already exists. Running
uv initin a directory that already has apyproject.tomlfails. Edit the existing file instead. - Invalid name. Names must be valid package names. Use letters, digits, hyphens, underscores, and periods.
- Wrong layout for the goal. If you plan to publish or define a command, start with
--package. Adding a build system later is possible but means editingpyproject.tomlby hand. - Surprising
requires-python. uv writes a lower bound based on the interpreter it found. If you need to support older versions, lower it deliberately. The next chapter covers this.
Exercise
You run uv init --lib mathkit. Which two things does this give you that plain uv init mathkit does not?
Show answer
A src/mathkit/ package layout (with a py.typed marker) and a [build-system] table, so the project is installed into its environment and can be built and imported by others.
Exercise
Right after uv init, you look for uv.lock and .venv and find neither. Is something wrong?
Show answer
No. uv init only writes the project files. uv creates the environment and lockfile the first time you run uv add, uv sync, or uv run.