Pants: Build System Commands Reference
Table of Contents
Section titled “Table of Contents”- Pantsbuild Usage
Pantsbuild Usage
Section titled “Pantsbuild Usage”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.
Basic Commands
Section titled “Basic Commands”List All Targets
Section titled “List All Targets”pants list ::List targets in a specific directory:
pants list src/python/myapp::Run Tests
Section titled “Run Tests”Run all tests:
pants test ::Run tests for a specific target:
pants test src/python/myapp:testsRun tests with coverage:
pants test --coverage-py-report=html ::Format Code
Section titled “Format Code”Format all code:
pants fmt ::Format specific files:
pants fmt src/python/myapp/main.pyLint Code
Section titled “Lint Code”pants lint ::Lint specific targets:
pants lint src/python/myapp::Type Checking
Section titled “Type Checking”pants check ::Build Package
Section titled “Build Package”pants package ::Build specific target:
pants package src/python/myapp:appRun Application
Section titled “Run Application”pants run src/python/myapp:mainCommon Configuration
Section titled “Common Configuration”pants.toml
Section titled “pants.toml”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 = trueresolves = { python-default = "3rdparty/python/default.lock" }
[python-infer]# Enable imports inferenceimports = trueinits = true
[black]config = "pyproject.toml"
[isort]config = [".isort.cfg"]
[mypy]config = "mypy.ini"BUILD File Example
Section titled “BUILD File Example”# 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",)Advanced Features
Section titled “Advanced Features”Dependencies Between Targets
Section titled “Dependencies Between Targets”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", ],)Multiple Python Resolves
Section titled “Multiple Python Resolves”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",)Custom Tool Configuration
Section titled “Custom Tool Configuration”Override tool versions:
[black]version = "black==23.3.0"args = ["--line-length=100"]
[pytest]args = ["-vv", "--tb=short"]Useful Workflows
Section titled “Useful Workflows”Pre-commit Hook
Section titled “Pre-commit Hook”Run Pants checks before commit:
pants --changed-since=HEAD lint checkCI/CD Integration
Section titled “CI/CD Integration”# In CI pipelinepants --changed-since=origin/main lint check testGenerate Lockfile
Section titled “Generate Lockfile”pants generate-lockfiles --resolve=python-defaultPeek at Target Metadata
Section titled “Peek at Target Metadata”pants peek src/python/myapp:libShow Dependencies
Section titled “Show Dependencies”pants dependencies src/python/myapp:libShow reverse dependencies (dependees):
pants dependees src/python/myapp:libPerformance Tips
Section titled “Performance Tips”- Use
--changed-sinceto only process changed files - Enable remote caching for shared builds
- Use
--filterto run specific types of targets - Leverage
pants.tomlto set project-wide defaults
Troubleshooting
Section titled “Troubleshooting”Clear Cache
Section titled “Clear Cache”pants clean-allDebug Target Resolution
Section titled “Debug Target Resolution”pants --print-stacktrace list src/python/myapp::Validate Configuration
Section titled “Validate Configuration”pants validate ::