Buck: python_library()

python_library()

This is liable to change in the future.

A python_library() rule is used to group together Python sources and resources to be packaged into a top-level python_binary rule.

Arguments

  • name (required) #

    The name of the rule.

  • srcs (defaults to []) #

    The set of .py files included 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 is an un-anchored regex (in java.util.regex.Pattern syntax) against which the platform name is matched (as defined in the `python#name` section of `.buckconfig`), and the second element is a list of source files.

  • resources (defaults to []) #

    Static files to be packaged along with the python sources. These resources can be accessed at runtime using the pkg_resources API.

  • platform_resources (defaults to []) #

    Python-platform-specific resource files. These should be specified as a list of pairs where the first element is an un-anchored regex (in java.util.regex.Pattern syntax) against which the platform name is matched (as defined in the `python#name` section of `.buckconfig`), and the second element is a list of resource files.

  • base_module (defaults to None) #

    The package for which the given specified sources and resources should reside in their final location in the top-level binary. If unset, the project relative directory that houses the BUCK file is used.

  • deps (defaults to []) #

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

  • visibility (defaults to []) #

    List of build target patterns that identify the build rules that can include this rule in its deps.

  • 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

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

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

Here is an example of using the `platform_srcs` and `platform_resources` parameters to pull in sources/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']),
  ],
)