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
-
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"], )
-
Import the runfiles library:
from python.runfiles import Runfiles
Pypi imports
-
Add the 'bazel-runfiles' dependency along with other third-party dependencies, for example in your
requirements.txtfile. -
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")], )
-
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()
# ...
Here my_workspace is the name you specified via module(name = "...") in your MODULE.bazel file (with --enable_bzlmod, default as of Bazel 7) or workspace(name = "...") in WORKSPACE (with --noenable_bzlmod).
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 that access runfiles, you have to 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file bazel_runfiles-1.8.4-py3-none-any.whl.
File metadata
- Download URL: bazel_runfiles-1.8.4-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c9c91d9f20f89642f00d533525f179586f8892af8aa451d71265824a6569582
|
|
| MD5 |
045da199931ab34533a82bb2138bc5a9
|
|
| BLAKE2b-256 |
0dd0b4f2bfcb88344f3beae54e7c91983f917bd549438ac0ffd0ceb0dbcaa5b0
|