Skip to main content

python_library

python_library

def python_library(
*,
name: str,
default_target_platform: None | str = None,
target_compatible_with: list[str] = [],
compatible_with: list[str] = [],
exec_compatible_with: list[str] = [],
visibility: list[str] = [],
within_view: list[str] = ["PUBLIC"],
metadata: OpaqueMetadata = {},
tests: list[str] = [],
modifiers: OpaqueMetadata = [],
_apple_platforms: dict[str, str] = {},
_create_manifest_for_source_dir: str = "prelude//python/tools:create_manifest_for_source_dir",
_create_third_party_build_root: str = "prelude//third-party/tools:create_build",
_cxx_toolchain: str = "gh_facebook_buck2_shims_meta//:cxx",
_python_internal_tools: str = "prelude//python/tools:python_internal_tools",
_python_toolchain: str = "gh_facebook_buck2_shims_meta//:python",
base_module: None | str = None,
contacts: list[str] = [],
default_host_platform: None | str = None,
deps: list[str] = [],
exclude_deps_from_merged_linking: bool = False,
ignore_compile_errors: bool = False,
labels: list[str] = [],
licenses: list[str] = [],
platform_deps: list[(str, list[str])] = [],
platform_resources: list[(str, list[str] | dict[str, str])] = [],
platform_srcs: list[(str, list[str] | dict[str, str])] = [],
py_version_for_type_checking: None | str = None,
resources: list[str] | dict[str, str] = [],
shard_typing: None | bool = None,
srcs: list[str] | dict[str, str] = [],
type_stubs: list[str] | dict[str, str] = [],
typing: bool = False,
versioned_resources: None | list[(dict[str, str], list[str] | dict[str, str])] = None,
versioned_srcs: None | list[(dict[str, str], list[str] | dict[str, str])] = None,
zip_safe: None | bool = None,
) -> None

A python_library() rule is used to group together Python source files and resources to be passed together in as a dep of other rules.

Parameters

  • name: name of the target
  • default_target_platform: specifies the default target platform, used when no platforms are specified on the command line
  • target_compatible_with: a list of constraints that are required to be satisfied for this target to be compatible with a configuration
  • compatible_with: a list of constraints that are required to be satisfied for this target to be compatible with a configuration
  • exec_compatible_with: a list of constraints that are required to be satisfied for this target to be compatible with an execution platform
  • visibility: a list of visibility patterns restricting what targets can depend on this one
  • within_view: a list of visibility patterns restricting what this target can depend on
  • metadata: a key-value map of metadata associated with this target
  • tests: a list of targets that provide tests for this one
  • modifiers: an array of modifiers associated with this target
  • base_module: 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: A list of python_library() rules that specify Python modules to include in the binary — including all transitive dependencies of these rules.
  • exclude_deps_from_merged_linking: When linking the top-level binary with a merged .buckconfig, 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.
  • labels: 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() .
  • platform_resources: 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 buckconfig#python section of .buckconfig.
  • platform_srcs: 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 buckconfig#python section of .buckconfig.
  • py_version_for_type_checking: This option will force the type checker to perform checking under a specific version of Python interpreter.
  • shard_typing: Determines if sharding should be enabled on a given target.
  • srcs: The set of Python (.py) files to include in this library.
  • typing: Determines whether to perform type checking on the given target. Default is False.

Details

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',
],
)