Skip to content

Run Python scripts#

Logic that is too complex to embed in pyproject.toml can be written in Python scripts in the bin directory. These can be called from aliases or from the command line with pw <script-name>.

The scripts run by default in the main tool context and hence can use all libraries installed in the main tool context.

Example#

Create a file bin/check-version.py:

"""Print the current project version from pyproject.toml."""
import tomllib
from pathlib import Path

pyproject = Path("pyproject.toml")
data = tomllib.loads(pyproject.read_text())
version = data["project"]["version"]
print(f"Current version: {version}")

You can invoke it directly:

px check-version
./pw check-version

Or reference it from an alias:

[tool.pyprojectx.aliases]
release = ["check-version", "uv build"]

Running scripts in a different tool context#

To run a script in a different tool context, you either:

  • define an alias:
[tool.pyprojectx.aliases]
# run the generate-data script in the 'jupyter' tool context
generate-data = { cmd = 'generate-data', ctx = 'jupyter' }
  • or specify the scripts_ctx in pyproject.toml (see recipes) :
[tool.pyprojectx]
scripts_ctx = "scripts"

Changing the scripts directory#

The default script directory (bin) can be changed by specifying the scripts_dir in pyproject.toml:

[tool.pyprojectx]
scripts_dir = "scripts"