Venv

Core Bazel rules for defining Python targets.

Rules

Functions

py_venv_binary

load("@rules_venv//python/venv:defs.bzl", "py_venv_binary")

py_venv_binary(name, deps, srcs, data, env, imports, main)

A py_venv_binary is an executable Python program consisting of a collection of .py source files (possibly belonging to other py_library rules), a *.runfiles directory tree containing all the code and data needed by the program at run-time, and a stub script that starts up the program with the correct initial environment and data.

load("@rules_venv//python/venv:defs.bzl", "py_venv_binary")

py_venv_binary(
    name = "foo",
    srcs = ["foo.py"],
    data = [":transform"],  # a cc_binary which we invoke at run time
    deps = [
        ":bar",  # a py_library
    ],
)

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsOther python targets to link to the current target.List of labelsoptional[]
srcsThe list of source (.py) files that are processed to create the target.List of labelsoptional[]
dataFiles needed by this rule at runtime. May list file or rule targets. Generally allows any target.List of labelsoptional[]
envDictionary of strings; values are subject to $(location) and "Make variable" substitution.Dictionary: String -> Stringoptional{}
importsList of import directories to be added to the PYTHONPATH.List of stringsoptional[]
mainThe name of the source file that is the main entry point of the application. This file must also be listed in srcs. If left unspecified, name is used instead. If name does not match any filename in srcs, main must be specified.LabeloptionalNone

py_venv_library

load("@rules_venv//python/venv:defs.bzl", "py_venv_library")

py_venv_library(name, deps, srcs, data, imports)

A library of Python code that can be depended upon.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsOther python targets to link to the current target.List of labelsoptional[]
srcsThe list of source (.py) files that are processed to create the target.List of labelsoptional[]
dataFiles needed by this rule at runtime. May list file or rule targets. Generally allows any target.List of labelsoptional[]
importsList of import directories to be added to the PYTHONPATH.List of stringsoptional[]

py_venv_test

load("@rules_venv//python/venv:defs.bzl", "py_venv_test")

py_venv_test(name, deps, srcs, data, env, env_inherit, imports, main)

A py_venv_test rule compiles a test. A test is a binary wrapper around some test code.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsOther python targets to link to the current target.List of labelsoptional[]
srcsThe list of source (.py) files that are processed to create the target.List of labelsoptional[]
dataFiles needed by this rule at runtime. May list file or rule targets. Generally allows any target.List of labelsoptional[]
envDictionary of strings; values are subject to $(location) and "Make variable" substitution.Dictionary: String -> Stringoptional{}
env_inheritSpecifies additional environment variables to inherit from the external environment when the test is executed by bazel test.List of stringsoptional[]
importsList of import directories to be added to the PYTHONPATH.List of stringsoptional[]
mainThe name of the source file that is the main entry point of the application. This file must also be listed in srcs. If left unspecified, name is used instead. If name does not match any filename in srcs, main must be specified.LabeloptionalNone

py_venv_toolchain

load("@rules_venv//python/venv:defs.bzl", "py_venv_toolchain")

py_venv_toolchain(name, zipapp_shebang)

Declare a toolchain for rules_venv rules.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
zipapp_shebangThe shebang to use when creating zipapps (OutputGroupInfo.python_zip_file).Stringoptional""

py_venv_zipapp

load("@rules_venv//python/venv:defs.bzl", "py_venv_zipapp")

py_venv_zipapp(name, args, binary, env, inherit_args, inherit_env, shebang)

A py_venv_zipapp is an executable Python zipapp which contains all of the dependencies and runfiles for a given executable.

load("@rules_venv//python/venv:defs.bzl", "py_venv_binary", "py_venv_zipapp")

py_venv_binary(
    name = "foo",
    srcs = ["foo.py"],
)

py_venv_zipapp(
    name = "foo_pyz",
    binary = ":foo",
)

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
argsArguments to add to the beginning of all invocations of the zipapp.List of stringsoptional[]
binaryThe binary to package as a zipapp.LabeloptionalNone
envEnvironment variables to inject into all invocations of the zipapp.Dictionary: String -> Stringoptional{}
inherit_argsInherit template variable expanded arguments from binary.BooleanoptionalFalse
inherit_envInherit template variable expanded environment variables from binary.BooleanoptionalFalse
shebangOptional shebang line to prepend to the zip (provided as content after #!).Stringoptional""

py_venv_common.create_dep_info

load("@rules_venv//python/venv:defs.bzl", "py_venv_common")

py_venv_common.create_dep_info(*, ctx, deps)

Construct dependency info required for building PyInfo

PARAMETERS

NameDescriptionDefault Value
ctxThe rule's context object.none
depsA list of python dependency targetsnone

RETURNS

struct: Dependency info.

py_venv_common.create_py_info

load("@rules_venv//python/venv:defs.bzl", "py_venv_common")

py_venv_common.create_py_info(*, ctx, imports, srcs, dep_info)

Construct a PyInfo provider

PARAMETERS

NameDescriptionDefault Value
ctxThe rule's context object.none
importsThe raw imports attribute.none
srcsA list of python (.py) source files.none
dep_infoDependency info from the current target.None

RETURNS

PyInfo: A PyInfo provider.

py_venv_common.create_runfiles_collection

load("@rules_venv//python/venv:defs.bzl", "py_venv_common")

py_venv_common.create_runfiles_collection(*, ctx, venv_toolchain, py_toolchain, runfiles,
                                          exclude_files, name, use_zip)

Generate a runfiles directory

This functionality exists due to the lack of native support for generating runfiles in an action. For details see: https://github.com/bazelbuild/bazel/issues/15486

PARAMETERS

NameDescriptionDefault Value
ctxThe rule's context object.none
venv_toolchainA py_venv_toolchain toolchain.none
py_toolchainA py_toolchain toolchain.none
runfilesThe runfiles to render into a directorynone
exclude_filesA collection of files to exclude from the collection despite them appearing in runfiles.depset([])
nameAn alternate name to use in the output instead of ctx.label.name.None
use_zipIf True, a zip file will be generated instead of a json manifest.False

RETURNS

Tuple[File, Runfiles]: The generated runfiles collection and associated runfiles.

py_venv_common.create_venv_attrs

load("@rules_venv//python/venv:defs.bzl", "py_venv_common")

py_venv_common.create_venv_attrs()

py_venv_common.create_venv_config_info

load("@rules_venv//python/venv:defs.bzl", "py_venv_common")

py_venv_common.create_venv_config_info(*, label, name, imports)

Construct info used to create venvs.

PARAMETERS

NameDescriptionDefault Value
labelThe label of the target that owns the venv.none
nameThe name for the venv.none
importsA list of import paths to write to .pth files.none

RETURNS

struct: the data.

py_venv_common.create_venv_entrypoint

load("@rules_venv//python/venv:defs.bzl", "py_venv_common")

py_venv_common.create_venv_entrypoint(*, ctx, venv_toolchain, py_info, main, runfiles, py_toolchain,
                                      name, use_runfiles_in_entrypoint, force_runfiles)

Create an executable which constructs a python venv and subprocesses a given entrypoint.

PARAMETERS

NameDescriptionDefault Value
ctxThe rule's context object.none
venv_toolchainA py_venv_toolchain toolchain.none
py_infoThe PyInfo provider for the current target.none
mainThe main python entrypoint.none
runfilesRunfiles associated with the executable.none
py_toolchainA py_toolchain toolchain. If one is not provided one will be acquired via py_venv_toolchain.None
nameAn alternate name to use in the output instead of ctx.label.name.None
use_runfiles_in_entrypointIf true, an entrypoint will be created that relies on runfiles.True
force_runfilesIf True, a rendered runfiles directory will be used over builtin runfiles where RUNFILES_DIR would be provided.False

RETURNS

Tuple[File, Runfiles]: The generated entrypoint and associated runfiles.

py_venv_common.get_toolchain

load("@rules_venv//python/venv:defs.bzl", "py_venv_common")

py_venv_common.get_toolchain(ctx, *, cfg)

PARAMETERS

NameDescriptionDefault Value
ctx

-

none
cfg

-

"target"