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

python_library()

This is liable to change in the future.

A python_library() rule is used to group together Python source files and resources to package them into a PEX using a top-level python_binary rule.

Arguments

  • name (required) #

    The short name for this build target.

  • srcs (defaults to []) #

    The set of Python (.py) files to include in this library.

  • platform_srcs (defaults to []) #

    Python-platform-specific source files. These should be specified as a list of pairs where the first element in each pair is an un-anchored regex against which the platform name is matched, and the second element is a list of source files. The regex should use java.util.regex.Pattern syntax. The platform name is a Python platform flavor defined in the [python] section of .buckconfig.

  • resources (defaults to []) #

    Static files to be packaged along with the Python sources. These resources can be accessed at runtime using the pkg_resources module distributed with Python's setuptools.

  • platform_resources (defaults to []) #

    Python-platform-specific resource files. These should be specified as a list of pairs where the first element in each pair is an un-anchored regex against which the platform name is matched, and the second element is a list of resource files. The regex should use java.util.regex.Pattern syntax. The platform name is a Python platform flavor defined in the [python] section of .buckconfig.

  • base_module (defaults to None) #

    The package in which the specified source files and resources should reside in their final location in the top-level binary. If unset, Buck uses the project-relative directory that contains the BUCK file.

  • deps (defaults to []) #

    Other python_library() rules that list srcs from which this rule imports modules.

  • exclude_deps_from_merged_linking (defaults to False) #

    When linking the top-level binary with a merged [python].native_link_strategy, do not merge or re-link any native transitive deps of this library. This is useful if this library wraps prebuilt native extensions which cannot be re-linked as part of library merging.

  • 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

Include Python source files and resource files.

# BUCK 

# A rule that includes a single Python file.
python_library(
  name = 'fileutil',
  srcs = ['fileutil.py'],
  deps = [
    '//third_party/python-magic:python-magic',
  ],
)

# A rule that uses glob() to include all Python source files in the
# directory in which the rule is defined. The rule also specifies a 
# resource file that gets packaged with the source file.
python_library(
  name = 'testutil',
  srcs = glob(['testutil/**/*.py']),
  resources = [
    'testdata.dat',
  ],
)

Use the platform_srcs and platform_resources arguments to pull in source files and resources only when building for a specific Python platform.

; .buckconfig

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

[python#py3]
  interpreter = /usr/bin/python3.4
# BUCK

python_library(
  name = 'utils',
  platform_srcs = [
    ('py2', ['foo.py']),
    ('py3', ['bar.py']),
  ],
  platform_resources = [
    ('py2', ['foo.dat']),
    ('py3', ['bar.dat']),
  ],
)