1*60517a1eSAndroid Build Coastguard Worker# bazel-runfiles library 2*60517a1eSAndroid Build Coastguard Worker 3*60517a1eSAndroid Build Coastguard WorkerThis is a Bazel Runfiles lookup library for Bazel-built Python binaries and tests. 4*60517a1eSAndroid Build Coastguard Worker 5*60517a1eSAndroid Build Coastguard WorkerLearn about runfiles: read [Runfiles guide](https://bazel.build/extending/rules#runfiles) 6*60517a1eSAndroid Build Coastguard Workeror watch [Fabian's BazelCon talk](https://www.youtube.com/watch?v=5NbgUMH1OGo). 7*60517a1eSAndroid Build Coastguard Worker 8*60517a1eSAndroid Build Coastguard Worker## Importing 9*60517a1eSAndroid Build Coastguard Worker 10*60517a1eSAndroid Build Coastguard WorkerThe Runfiles API is available from two sources, a direct Bazel target, and a [pypi](https://pypi.org/) package. 11*60517a1eSAndroid Build Coastguard Worker 12*60517a1eSAndroid Build Coastguard Worker## Pure Bazel imports 13*60517a1eSAndroid Build Coastguard Worker 14*60517a1eSAndroid Build Coastguard Worker1. Depend on this runfiles library from your build rule, like you would other third-party libraries: 15*60517a1eSAndroid Build Coastguard Worker 16*60517a1eSAndroid Build Coastguard Worker ```python 17*60517a1eSAndroid Build Coastguard Worker py_binary( 18*60517a1eSAndroid Build Coastguard Worker name = "my_binary", 19*60517a1eSAndroid Build Coastguard Worker # ... 20*60517a1eSAndroid Build Coastguard Worker deps = ["@rules_python//python/runfiles"], 21*60517a1eSAndroid Build Coastguard Worker ) 22*60517a1eSAndroid Build Coastguard Worker ``` 23*60517a1eSAndroid Build Coastguard Worker 24*60517a1eSAndroid Build Coastguard Worker2. Import the runfiles library: 25*60517a1eSAndroid Build Coastguard Worker 26*60517a1eSAndroid Build Coastguard Worker ```python 27*60517a1eSAndroid Build Coastguard Worker from python.runfiles import Runfiles 28*60517a1eSAndroid Build Coastguard Worker ``` 29*60517a1eSAndroid Build Coastguard Worker 30*60517a1eSAndroid Build Coastguard Worker## Pypi imports 31*60517a1eSAndroid Build Coastguard Worker 32*60517a1eSAndroid Build Coastguard Worker1. Add the 'bazel-runfiles' dependency along with other third-party dependencies, for example in your `requirements.txt` file. 33*60517a1eSAndroid Build Coastguard Worker 34*60517a1eSAndroid Build Coastguard Worker2. Depend on this runfiles library from your build rule, like you would other third-party libraries: 35*60517a1eSAndroid Build Coastguard Worker ```python 36*60517a1eSAndroid Build Coastguard Worker load("@pip_deps//:requirements.bzl", "requirement") 37*60517a1eSAndroid Build Coastguard Worker 38*60517a1eSAndroid Build Coastguard Worker py_binary( 39*60517a1eSAndroid Build Coastguard Worker name = "my_binary", 40*60517a1eSAndroid Build Coastguard Worker ... 41*60517a1eSAndroid Build Coastguard Worker deps = [requirement("bazel-runfiles")], 42*60517a1eSAndroid Build Coastguard Worker ) 43*60517a1eSAndroid Build Coastguard Worker ``` 44*60517a1eSAndroid Build Coastguard Worker 45*60517a1eSAndroid Build Coastguard Worker3. Import the runfiles library: 46*60517a1eSAndroid Build Coastguard Worker ```python 47*60517a1eSAndroid Build Coastguard Worker from runfiles import Runfiles 48*60517a1eSAndroid Build Coastguard Worker ``` 49*60517a1eSAndroid Build Coastguard Worker 50*60517a1eSAndroid Build Coastguard Worker## Typical Usage 51*60517a1eSAndroid Build Coastguard Worker 52*60517a1eSAndroid Build Coastguard WorkerCreate a `Runfiles` object and use `Rlocation` to look up runfile paths: 53*60517a1eSAndroid Build Coastguard Worker 54*60517a1eSAndroid Build Coastguard Worker```python 55*60517a1eSAndroid Build Coastguard Workerr = Runfiles.Create() 56*60517a1eSAndroid Build Coastguard Worker# ... 57*60517a1eSAndroid Build Coastguard Workerwith open(r.Rlocation("my_workspace/path/to/my/data.txt"), "r") as f: 58*60517a1eSAndroid Build Coastguard Worker contents = f.readlines() 59*60517a1eSAndroid Build Coastguard Worker # ... 60*60517a1eSAndroid Build Coastguard Worker``` 61*60517a1eSAndroid Build Coastguard Worker 62*60517a1eSAndroid Build Coastguard WorkerThe code above creates a manifest- or directory-based implementation based on the environment variables in `os.environ`. See `Runfiles.Create()` for more info. 63*60517a1eSAndroid Build Coastguard Worker 64*60517a1eSAndroid Build Coastguard WorkerIf you want to explicitly create a manifest- or directory-based 65*60517a1eSAndroid Build Coastguard Workerimplementation, you can do so as follows: 66*60517a1eSAndroid Build Coastguard Worker 67*60517a1eSAndroid Build Coastguard Worker```python 68*60517a1eSAndroid Build Coastguard Workerr1 = Runfiles.CreateManifestBased("path/to/foo.runfiles_manifest") 69*60517a1eSAndroid Build Coastguard Worker 70*60517a1eSAndroid Build Coastguard Workerr2 = Runfiles.CreateDirectoryBased("path/to/foo.runfiles/") 71*60517a1eSAndroid Build Coastguard Worker``` 72*60517a1eSAndroid Build Coastguard Worker 73*60517a1eSAndroid Build Coastguard WorkerIf you want to start subprocesses, and the subprocess can't automatically 74*60517a1eSAndroid Build Coastguard Workerfind the correct runfiles directory, you can explicitly set the right 75*60517a1eSAndroid Build Coastguard Workerenvironment variables for them: 76*60517a1eSAndroid Build Coastguard Worker 77*60517a1eSAndroid Build Coastguard Worker```python 78*60517a1eSAndroid Build Coastguard Workerimport subprocess 79*60517a1eSAndroid Build Coastguard Workerfrom python.runfiles import Runfiles 80*60517a1eSAndroid Build Coastguard Worker 81*60517a1eSAndroid Build Coastguard Workerr = Runfiles.Create() 82*60517a1eSAndroid Build Coastguard Workerenv = {} 83*60517a1eSAndroid Build Coastguard Worker# ... 84*60517a1eSAndroid Build Coastguard Workerenv.update(r.EnvVars()) 85*60517a1eSAndroid Build Coastguard Workerp = subprocess.run( 86*60517a1eSAndroid Build Coastguard Worker [r.Rlocation("path/to/binary")], 87*60517a1eSAndroid Build Coastguard Worker env=env, 88*60517a1eSAndroid Build Coastguard Worker # ... 89*60517a1eSAndroid Build Coastguard Worker) 90*60517a1eSAndroid Build Coastguard Worker``` 91