Sub topic 9
Packaging code and defining command-line entry points
This chapter assumes you can create a project with uv init, read a pyproject.toml file, and add dependencies with uv add. Here you will turn a project into an installable package and give it a command that runs from the terminal.
Why package your code
A project that only holds scripts can run from its own folder. A package can be installed into an environment, imported by name, and started with a command. uv installs a package project into its environment in editable mode, so your edits take effect without reinstalling.
The src layout
The --package flag of uv init creates a src layout:
uv init --package weather-cli
cd weather-cliweather-cli/
├── pyproject.toml
├── README.md
└── src/
└── weather_cli/
└── __init__.pyThe project name uses a hyphen, and the import name uses an underscore. Keep your code under src/ so tests import the installed package and not stray files in the working directory.
Defining an entry point
An entry point maps a command name to a Python function. You declare it in the [project.scripts] table:
[project.scripts]
weather = "weather_cli:main"The value has the form module:function. Here weather_cli:main means the main function in the weather_cli package. When the package is installed, the installer creates an executable named weather that calls this function.
The function lives in src/weather_cli/__init__.py:
import typer
def main() -> None:
typer.echo("Sunny")
if __name__ == "__main__":
typer.run(main)Running the command
Run the command inside the project environment:
uv run weatheruv syncs the environment first, installs the package, and then runs the weather executable. The output is Sunny.
Common errors
- Command not found. The package is not installed in the active environment. Use
uv run weatheror runuv syncfirst. - ModuleNotFoundError for your package. The module name in the entry point does not match the folder under
src/. Use the underscore name, such asweather_cli. - AttributeError for
main. The function named after the colon does not exist in that module. Check the spelling. - Entry point changes are ignored. The executable is generated at install time. After you edit
[project.scripts], runuv syncoruv run. If the old command still runs, useuv sync --reinstall-package weather-cli.
Check your understanding
Exercise
Your project is named data-tools and has a function run in src/data_tools/cli.py. Write the [project.scripts] entry that creates a command called dtools.
Show answer
[project.scripts] dtools = "data_tools.cli:run" The module path uses the underscore import name and the dotted path to the module, followed by a colon and the function name.
Exercise
You added a [project.scripts] entry to a project made with plain uv init, but uv run cannot find the command. Why, and how do you fix it?
Show answer
Plain uv init creates an application without a [build-system] table, so uv does not install the project and no executable is generated. Add a build system such as hatchling, or recreate the project with uv init --package, then run uv sync.