Pex

Bazel rules for the Python executable packaging tool pex.

Setup

bazel_dep(name = "rules_pex", version = "{version}")

pex = use_extension("//pex:extensions.bzl", "pex")
pex.toolchain(
    name = "pex_toolchains",
)

use_repo(
    pex,
    "pex_toolchains",
)

register_toolchains(
    "@pex_toolchains//:all",
    dev_dependency = True,
)

Rules

Module Extensions

py_pex_binary

load("@rules_pex//pex:defs.bzl", "py_pex_binary")

py_pex_binary(name, binary)

A rule for creating pex executables from Python targets.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
binaryThe py_binary target to convert to a Pex.Labelrequired

py_pex_toolchain

load("@rules_pex//pex:defs.bzl", "py_pex_toolchain")

py_pex_toolchain(name, pex, platform, scie_jump, scie_ptex, scie_python_interpreter,
                 scie_python_version, scie_science)

A toolchain for the pex packaging tool rules.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
pexThe pex binary to use with the rules.Labelrequired
platformThe platform to target for scie executables.Stringrequired
scie_jumpThe scie jump binary to use for scie targets.Labelrequired
scie_ptexThe scie ptex binary to use for scie targets.Labelrequired
scie_python_interpreterThe standalone python interpreter archive (.tar.gz) to bundle into scie binaries.Labelrequired
scie_python_versionThe Python version string to pass to pex for scie builds (e.g., '3.11').Stringrequired
scie_scienceThe scie science binary to use for scie targets.Labelrequired

py_scie_binary

load("@rules_pex//pex:defs.bzl", "py_scie_binary")

py_scie_binary(name, binary)

A rule for creating pex scie executables from Python targets.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
binaryThe py_binary target to convert to a Pex.Labelrequired

pex

pex = use_extension("@rules_pex//pex:defs.bzl", "pex")
pex.add_fallback_windows_binaries(toolchain_name)
pex.toolchain(name, pex_version, python_version, scie_jump_version, scie_ptex_version,
              scie_science_version)

Bzlmod extensions for Pex.

TAG CLASSES

add_fallback_windows_binaries

Registers Windows fallback binaries for a specific toolchain.

This tag is used to add fallback binaries for Windows platforms when the actual pex tools don't support Windows. This prevents toolchain resolution failures in multi-platform repositories.

Note: Full Windows support is tracked in https://github.com/UebelAndre/rules_pex/issues/1

Example:

pex = use_extension("//pex:extensions.bzl", "pex")
pex.toolchain(
    name = "pex_toolchains",
    python_version = "3.11",
)
pex.add_fallback_windows_binaries(
    toolchain_name = "pex_toolchains",
)
use_repo(pex, "pex_toolchains")
register_toolchains("@pex_toolchains//:all")

Attributes

NameDescriptionTypeMandatoryDefault
toolchain_nameThe name of the toolchain to add Windows fallback binaries for.Stringrequired

toolchain

An extension for defining a pex toolchain.

An example of defining and using a pex toolchain:

pex = use_extension("//pex:extensions.bzl", "pex")
pex.toolchain(
    name = "pex_toolchains",
    pex_version = "2.67.3",
    python_version = "3.11",
    scie_science_version = "0.15.0",
    scie_jump_version = "0.15.0",
    scie_ptex_version = "0.15.0",
)
use_repo(pex, "pex_toolchains")
register_toolchains(
    "@pex_toolchains//:all"
)

Attributes

NameDescriptionTypeMandatoryDefault
nameThe name of the repository hub to create.Namerequired
pex_versionThe version of pex to download.Stringoptional"2.67.3"
python_versionThe Python version (e.g., '3.11').Stringoptional"3.11"
scie_jump_versionThe version of scie jump to download.Stringoptional"1.8.0"
scie_ptex_versionThe version of scie ptex to download.Stringoptional"1.7.0"
scie_science_versionThe version of scie science to download.Stringoptional"0.15.0"