Skip to content

How to access data runfiles from Python? #2545

Closed

Activity

  1. added
    P4
    on Feb 20, 2017
  2. ajayinduramesh commented on Mar 18, 2017

    @ajayinduramesh

    Hi ,

    I am participating in GSoC-2017.
    I am trying to getting drenched in Bazel ecosystem for contributions.
    Please, can someone assign this issue to me?

    Thank you
    Ajay 👨🏻‍💻

  3. breakds commented on Aug 9, 2017

    @breakds

    I think it is a much needed feature to access the runfiles (probably the reason why we even have data= field in py_library and py_binary). I am wondering why it is marked as P4?

  4. self-assigned this
    on Oct 17, 2018
  5. brandjon commented on Oct 17, 2018

    @brandjon
    Member

    Better semantics/documentation around runfiles is something I'd like to get to soonish. In any case it's not P4.

  6. added
    P3
    team-Rules-Python
    and removed
    P4
    on Oct 17, 2018
  7. dsculptor commented on Dec 13, 2019

    @dsculptor

    Is this really a documentation request? And why is it P3?

    Has there been thought given to how to migrate python code which access files "in normal way" -> move to bazel -> access files via runfiles library?

    Can we please get the ultimate example for a python project which access a bunch of runfiles?

  8. keith commented on Aug 17, 2020

    @keith
    Member

    For others who find this I found the header document here useful

    USAGE:
    1. Depend on this runfiles library from your build rule:
    py_binary(
    name = "my_binary",
    ...
    deps = ["@rules_python//python/runfiles"],
    )
    2. Import the runfiles library.
    from rules_python.python.runfiles import runfiles
    3. 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 implementations based
    on the environment variables in os.environ. See `Create()` for more info.
    If you want to explicitly create a manifest- or directory-based
    implementations, 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 also need runfiles, you need to set
    the right environment variables for them:
    import subprocess
    from rules_python.python.runfiles import runfiles
    r = runfiles.Create()
    env = {}
    ...
    env.update(r.EnvVars())
    p = subprocess.Popen([r.Rlocation("path/to/binary")], env, ...)

  9. added
    P4
    and removed
    P3
    on Nov 18, 2020
  10. added
    team-Documentation
    on Jan 11, 2023
  11. github-actions commented on Mar 17, 2024

    @github-actions

    Thank you for contributing to the Bazel repository! This issue has been marked as stale since it has not had any activity in the last 1+ years. It will be closed in the next 90 days unless any other activity occurs. If you think this issue is still relevant and should stay open, please post any comment here and the issue will no longer be marked as stale.

  12. added
    stale
    on Mar 17, 2024
  13. fmeum commented on Mar 17, 2024

    @fmeum
    Collaborator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P4staleteam-Documentationteam-Rules-Pythontype: documentation (cleanup)

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions