Buck: python_binary()
Support Ukraine. Help Provide Humanitarian Aid to Ukraine.

python_binary()

This is liable to change in the future.

A python_binary() rule is used to build a PEX file—an executable Python package—that includes Python sources and resources from all transitive python_library dependencies.

Arguments

  • name (required) #

    The short name for this build target. Also, the output PEX file will have this name as its base filename with an additional .pex extension.

  • main_module (required) #

    The python module that should be the entry point of the binary. This should be a module name within a python_library that this binary depends on. Note that module names take base_module of the library into account. This property is mutually exclusive with main, and should be preferred to main, which is deprecated.

  • main (defaults to None) #

    This property is deprecated. Use a python_library dependency and main_module instead

    The Python file which serves as the entry point for the PEX. The interpreter initiates execution of the PEX with the code in this file.

  • base_module (defaults to None) #

    This property is deprecated.

    The package in which the main module should reside in its final location in the binary. If unset, Buck uses the project-relative directory that contains the BUCK file.

  • platform (defaults to None) #

    The name of the Python platform flavor to build against by default as defined in the [python] section of .buckconfig.

  • deps (defaults to []) #

    A list of python_library rules that specify Python modules to include in the PEX file—including all transitive dependencies of these rules.

  • preload_deps (defaults to []) #

    A list of C/C++ library dependencies that need to be loaded before any other libraries when the PEX starts up. This requires dynamic loader support, such as LD_PRELOAD, found on most systems. This list is order- sensitive and the preload libraries listed here are passed down to the dynamic linker in the same order.

  • package_style (defaults to None) #

    Used to override the global packaging style that is set in [[python].package_style].

  • linker_flags (defaults to []) #

    Additional linker flags that should be applied to any linking which is specific to this rule. Note that whether these flags are used is dependent on the native link strategy selected in.buckconfig and currently applies only to the merged [python].native_link_strategy; the separate link strategy pulls in shared libraries that are linked in the context of the rules that own them, such as cxx_library.

  • visibility (defaults to []) #

    List of build target patterns that identify the build rules that can include this rule as a dependency, for example, by listing it in their deps or exported_deps attributes. For more information, see visibility.

  • licenses (defaults to []) #

    Set of license files for this library. To get the list of license files for a given build rule and all of its dependencies, you can use buck query.

  • labels (defaults to []) #

    Set of arbitrary strings which allow you to annotate a build rule with tags that can be searched for over an entire dependency tree using buck query attrfilter().

Examples

Build a PEX from the Python files in the BUCK directory.

# BUCK

python_binary(
  name = 'tailer',
  main_module = 'tailer',
  deps = [
    ':tailerutils',
  ],
)

python_library(
  name = 'tailerutils',
  # The main module, tailer.py, is specified here.
  # (Separated out from the glob pattern for clarity.)
  srcs = glob(['tailer.py', '*.py']),
)

Use the platform argument to select the [python#py2] platform as defined in .buckconfig.

; .buckconfig

[python#py2]
  interpreter = /usr/bin/python2.7
# BUCK

python_binary(
  name = 'bin',
  platform = 'py2',
  main_module = 'main',
  deps = [
    ':bar',
  ],
)