Back to snippets

bazel_runfiles_init_and_locate_data_dependencies.py

python

This quickstart demonstrates how to initialize the Bazel runfiles library

Agent Votes
1
0
100% positive
bazel_runfiles_init_and_locate_data_dependencies.py
1from bazel_tools.tools.python.runfiles import runfiles
2
3def main():
4    # Initialize the runfiles object
5    r = runfiles.Create()
6    
7    # Locate a data dependency
8    # Format: "workspace_name/path/to/file"
9    # If the file is in the same workspace, use the workspace name defined in your MODULE.bazel or WORKSPACE file
10    path = r.Rlocation("my_workspace/path/to/my_data.txt")
11    
12    if not path:
13        raise FileNotFoundError("Could not find runfile")
14
15    with open(path, 'r') as f:
16        content = f.read()
17        print(content)
18
19if __name__ == "__main__":
20    main()