Skip to content

Pants: Build System Commands Reference

Pants is a fast, scalable, user-friendly build system for codebases of all sizes. It orchestrates dozens of standard tools and provides unified interface for developers.

Terminal window
pants list ::

List targets in a specific directory:

Terminal window
pants list src/python/myapp::

Run all tests:

Terminal window
pants test ::

Run tests for a specific target:

Terminal window
pants test src/python/myapp:tests

Run tests with coverage:

Terminal window
pants test --coverage-py-report=html ::

Format all code:

Terminal window
pants fmt ::

Format specific files:

Terminal window
pants fmt src/python/myapp/main.py
Terminal window
pants lint ::

Lint specific targets:

Terminal window
pants lint src/python/myapp::
Terminal window
pants check ::
Terminal window
pants package ::

Build specific target:

Terminal window
pants package src/python/myapp:app
Terminal window
pants run src/python/myapp:main

Basic configuration file structure:

[GLOBAL]
pants_version = "2.20.0"
backend_packages = [
"pants.backend.python",
"pants.backend.python.lint.black",
"pants.backend.python.lint.isort",
"pants.backend.python.typecheck.mypy",
]
[python]
interpreter_constraints = ["==3.11.*"]
enable_resolves = true
resolves = { python-default = "3rdparty/python/default.lock" }
[python-infer]
# Enable imports inference
imports = true
inits = true
[black]
config = "pyproject.toml"
[isort]
config = [".isort.cfg"]
[mypy]
config = "mypy.ini"
# src/python/myapp/BUILD
python_sources(
name="lib",
)
python_tests(
name="tests",
sources=["*_test.py", "test_*.py"],
)
pex_binary(
name="app",
entry_point="main.py",
)

Pants automatically infers dependencies from imports, but you can also explicitly declare them:

python_sources(
name="lib",
dependencies=[
"src/python/otherapp:lib",
"3rdparty/python#requests",
],
)

Define multiple lock files for different parts of your codebase:

[python]
resolves = {
python-default = "3rdparty/python/default.lock",
python-data-science = "3rdparty/python/data-science.lock",
}

Then specify which resolve to use:

python_sources(
name="lib",
resolve="python-data-science",
)

Override tool versions:

[black]
version = "black==23.3.0"
args = ["--line-length=100"]
[pytest]
args = ["-vv", "--tb=short"]

Run Pants checks before commit:

Terminal window
pants --changed-since=HEAD lint check
Terminal window
# In CI pipeline
pants --changed-since=origin/main lint check test
Terminal window
pants generate-lockfiles --resolve=python-default
Terminal window
pants peek src/python/myapp:lib
Terminal window
pants dependencies src/python/myapp:lib

Show reverse dependencies (dependees):

Terminal window
pants dependees src/python/myapp:lib
  1. Use --changed-since to only process changed files
  2. Enable remote caching for shared builds
  3. Use --filter to run specific types of targets
  4. Leverage pants.toml to set project-wide defaults
Terminal window
pants clean-all
Terminal window
pants --print-stacktrace list src/python/myapp::
Terminal window
pants validate ::