Skip to main content

How to Add a Command to a Python Project with [project.scripts]

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A Python project can come with its own commands, so you type uv run greet instead of uv run python src/greeter/cli.py. You declare each command in one table of pyproject.toml, [project.scripts], by pointing a name at a Python function. This guide adds a greet command with uv 0.13.10 on Python 3.15.2.

Syntax​

Syntax
[project.scripts]
<command> = "<package>.<module>:<function>"
PartWhat it means
<command>The name you type to run it, for example greet
<package>.<module>The Python module that holds the function, written the way you'd import it
<function>The function that runs when you type the command. It takes no arguments

The uv documentation says: "Projects may define command line interfaces (CLIs) for the project in the [project.scripts] table of the pyproject.toml." uv installs each one into the project's environment, where uv run <command> finds it.

The Example​

A project named greeter gets a greet command that prints a greeting.

Prerequisites​

  • uv. This guide uses uv 0.13.10, which also uses Python 3.15.2 here.

Step 1: Create the Project​

Terminal
uv init --python 3.15 greeter
Output
Initialized project `greeter` at `/Users/you/source/greeter`

uv init makes a package project with its code in src/greeter/, and already gives it one command, also named greeter. You'll replace it. Move into the folder:

Terminal
cd greeter

Step 2: Write the Function​

Create src/greeter/cli.py with the function the command will run:

src/greeter/cli.py
def main() -> None:
print("Hello from the greet command!")

The src/greeter/cli.py file in a macOS-style code window: a main function that prints Hello from the greet command!

The function takes no arguments: uv calls it with none. A command that needs input reads it inside the function, for example with argparse.

Step 3: Point a Command at It​

Open pyproject.toml. Its [project.scripts] table has the command uv init made, greeter = "greeter:main". Replace that line, so the table reads:

pyproject.toml
[project.scripts]
greet = "greeter.cli:main"

The [project.scripts] table of pyproject.toml in a macOS-style code window, with greet = "greeter.cli".

PartHere
greetThe command's name
greeter.cliThe module src/greeter/cli.py, written as an import
mainThe function in that module

Step 4: Run the Command​

Terminal
uv run greet
Output
Using CPython 3.15.2 interpreter at: /Library/Frameworks/Python.framework/Versions/3.15/bin/python3.15
Creating virtual environment at: .venv
Built greeter @ file:///Users/you/source/greeter
Installed 1 package in 2ms
Hello from the greet command!

The terminal after uv run greet: uv created the virtual environment, built and installed greeter, and the command printed Hello from the greet command!

On this first run uv created the environment and built the project, which installed the greet command, then ran main(). Each time pyproject.toml changes, the next uv run builds the project again, so a renamed or new command is ready without an extra step. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
A typo in the function name, greet = "greeter.cli:mian"uv builds fine, then the command fails: ImportError: cannot import name 'mian' from 'greeter.cli' (/Users/you/source/greeter/src/greeter/cli.py)Match the name after : to the function exactly
Typing the old command after renaming it: uv run greetererror: Failed to spawn: greeter with cause: No such file or directory (os error 2), because that command no longer existsUse the new name, uv run greet
Writing a file path, greet = "src/greeter/cli.py:main"The build fails: error: Failed to install: greeter-0.1.0-py3-none-any.whl with cause: The wheel is invalid: invalid console script: 'src/greeter/cli.py:main'Write the import path, with dots and without src/: greeter.cli:main

FAQ​

Can a project have several commands?​

Yes, one line each in [project.scripts]. Each points at its own function, or two names can point at the same one.

How does the command get its arguments?​

The function reads them itself. sys.argv[1:] holds what was typed after the command name, and argparse turns that into named options.

Conclusion​

One line in [project.scripts], greet = "greeter.cli:main", turned the main() function into a greet command, and uv run greet printed its greeting.

Additional Resources​