• Home
Name Date Size #Lines LOC

..--

BUILD.bazelD06-Sep-20241.9 KiB5954

README.rstD06-Sep-20241.8 KiB5639

__init__.pyD06-Sep-2024628 161

runfiles.pyD06-Sep-202414.3 KiB369235

README.rst

1bazel-runfiles library
2======================
3
4This is a Bazel Runfiles lookup library for Bazel-built Python binaries and tests.
5
6Learn about runfiles: read `Runfiles guide <https://bazel.build/extending/rules#runfiles>`_
7or watch `Fabian's BazelCon talk <https://www.youtube.com/watch?v=5NbgUMH1OGo>`_.
8
9Typical Usage
10-------------
11
121.  Add the 'bazel-runfiles' dependency along with other third-party dependencies, for example in your
13    ``requirements.txt`` file.
14
152.  Depend on this runfiles library from your build rule, like you would other third-party libraries::
16
17      py_binary(
18          name = "my_binary",
19          ...
20          deps = [requirement("bazel-runfiles")],
21      )
22
233.  Import the runfiles library::
24
25      import runfiles  # not "from runfiles import runfiles"
26
274.  Create a Runfiles object and use rlocation to look up runfile paths::
28
29      r = runfiles.Create()
30      ...
31      with open(r.Rlocation("my_workspace/path/to/my/data.txt"), "r") as f:
32        contents = f.readlines()
33        ...
34
35    The code above creates a manifest- or directory-based implementations based
36    on the environment variables in os.environ. See `Create()` for more info.
37
38    If you want to explicitly create a manifest- or directory-based
39    implementations, you can do so as follows::
40
41      r1 = runfiles.CreateManifestBased("path/to/foo.runfiles_manifest")
42
43      r2 = runfiles.CreateDirectoryBased("path/to/foo.runfiles/")
44
45    If you want to start subprocesses, and the subprocess can't automatically
46    find the correct runfiles directory, you can explicitly set the right
47    environment variables for them::
48
49      import subprocess
50      import runfiles
51
52      r = runfiles.Create()
53      env = {}
54      ...
55      env.update(r.EnvVars())
56      p = subprocess.Popen([r.Rlocation("path/to/binary")], env, ...)