Skip to main content

No project description provided

Project description

bazel-runfiles library

This is a Bazel Runfiles lookup library for Bazel-built Python binaries and tests.

Learn about runfiles: read Runfiles guide or watch Fabian's BazelCon talk.

Importing

The Runfiles API is available from two sources, a direct Bazel target, and a pypi package.

Pure Bazel imports

  1. Depend on this runfiles library from your build rule, like you would other third-party libraries:

    py_binary(
        name = "my_binary",
        # ...
        deps = ["@rules_python//python/runfiles"],
    )
    
  2. Import the runfiles library:

        from python.runfiles import Runfiles
    

Pypi imports

  1. Add the 'bazel-runfiles' dependency along with other third-party dependencies, for example in your requirements.txt file.

  2. Depend on this runfiles library from your build rule, like you would other third-party libraries:

    load("@pip_deps//:requirements.bzl", "requirement")
    
    py_binary(
        name = "my_binary",
        ...
        deps = [requirement("bazel-runfiles")],
    )
    
  3. Import the runfiles library:

    from runfiles import Runfiles
    

Typical Usage

Create a Runfiles object and use Rlocation to look up runfile paths:

r = Runfiles.Create()
# ...
with open(r.Rlocation("my_workspace/path/to/my/data.txt"), "r") as f:
    contents = f.readlines()
    # ...

The code above creates a manifest- or directory-based implementation based on the environment variables in os.environ. See Runfiles.Create() for more info.

If you want to explicitly create a manifest- or directory-based implementation, you can do so as follows:

r1 = Runfiles.CreateManifestBased("path/to/foo.runfiles_manifest")

r2 = Runfiles.CreateDirectoryBased("path/to/foo.runfiles/")

If you want to start subprocesses, and the subprocess can't automatically find the correct runfiles directory, you can explicitly set the right environment variables for them:

import subprocess
from python.runfiles import Runfiles

r = Runfiles.Create()
env = {}
# ...
env.update(r.EnvVars())
p = subprocess.run(
    [r.Rlocation("path/to/binary")],
    env=env,
    # ...
)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

bazel_runfiles-0.40.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file bazel_runfiles-0.40.0-py3-none-any.whl.

File metadata

File hashes

Hashes for bazel_runfiles-0.40.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7e4f10db67a37bc0639e81fc1f505b12b3bbc69657e375b35d9cffac2d692d1f
MD5 ae0caed095578175a5abd4fb11fad10b
BLAKE2b-256 a9f29881dbcd6c79c01714ca0024b5ec816ed2ba1a0d15b10da05876f80b830d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page