//python/entry_points:py_console_script_binary.bzl

Creates an executable (a non-test binary) for console_script entry points.

This rule is to make it easier to generate console_script entry points as per Python specification.

Generate a py_binary target for a particular console_script entry_point from a PyPI package, e.g. for creating an executable pylint target, use:

load("@rules_python//python/entry_points:py_console_script_binary.bzl", "py_console_script_binary")

py_console_script_binary(
    name = "pylint",
    pkg = "@pip//pylint",
)

Specifying extra dependencies

You can also specify extra dependencies and the exact script name you want to call. This is useful for tools like flake8, pylint, and pytest, which have plugin discovery methods and discover dependencies from the PyPI packages available in the PYTHONPATH.

load("@rules_python//python/entry_points:py_console_script_binary.bzl", "py_console_script_binary")

py_console_script_binary(
    name = "pylint_with_deps",
    pkg = "@pip//pylint",
    # Because `pylint` has multiple console_scripts available, we have to
    # specify which we want if the name of the target name 'pylint_with_deps'
    # cannot be used to guess the entry_point script.
    script = "pylint",
    deps = [
        # One can add extra dependencies to the entry point.
        # This specifically allows us to add plugins to pylint.
        "@pip//pylint_print",
    ],
)

Using a specific Python version

A specific Python version can be forced by passing the desired Python version, e.g. to force Python 3.9:

load("@rules_python//python/entry_points:py_console_script_binary.bzl", "py_console_script_binary")

py_console_script_binary(
    name = "yamllint",
    pkg = "@pip//yamllint",
    python_version = "3.9",
)

Adding a Shebang Line

You can specify a shebang line for the generated binary. This is useful for Unix-like systems where the shebang line determines which interpreter is used to execute the script, per PEP441:

load("@rules_python//python/entry_points:py_console_script_binary.bzl", "py_console_script_binary")

py_console_script_binary(
    name = "black",
    pkg = "@pip//black",
    shebang = "#!/usr/bin/env python3",
)

Note that to execute via the shebang line, you need to ensure the specified Python interpreter is available in the environment.

Using a specific Python Version directly from a Toolchain

Deprecated since version 1.1.0: The toolchain-specific py_binary and py_test symbols are aliases to the regular rules. For example, load("@python_versions//3.11:defs.bzl", "py_binary") and load("@python_versions//3.11:defs.bzl", "py_test") are deprecated.

You should instead specify the desired Python version with python_version; see the example above.

Alternatively, the py_console_script_binary.binary_rule arg can be passed the version-bound py_binary symbol, or any other py_binary-compatible rule of your choosing:

load("@python_versions//3.9:defs.bzl", "py_binary")
load("@rules_python//python/entry_points:py_console_script_binary.bzl", "py_console_script_binary")

py_console_script_binary(
    name = "yamllint",
    pkg = "@pip//yamllint:pkg",
    binary_rule = py_binary,
)
py_console_script_binary(name, pkg, entry_points_txt=None, script=None, binary_rule='<function py_binary from //python:py_binary.bzl>', shebang='', main=None, **kwargs)

Generate a py_binary for a console_script entry_point.

Args:
  • name(Name)

    The name of the resulting target.

  • pkg(Label)

    the package for which to generate the script.

  • entry_points_txt(label | None) (default None)

    , the entry_points.txt file to parse for available console_script values. It may be a single file, or a group of files, but must contain a file named entry_points.txt. If not specified, defaults to the dist_info target in the same package as the pkg Label.

  • script(str) (default None)

    , The console script name that the py_binary is going to be generated for. Defaults to the normalized name attribute.

  • binary_rule(callable) (default ‘<function py_binary from //python:py_binary.bzl>’)

    , The rule/macro to use to instantiate the target. It’s expected to behave like py_binary. Defaults to py_binary.

  • shebang(str) (default “”)

    , The shebang to use for the entry point python file. Defaults to empty string.

  • main(str) (default None)

    , the python file to be generated, defaults to <name>_entry_point.py to be compatible with symbolic macros.

  • kwargs – Extra parameters forwarded to binary_rule.