https://pantsbuild.org/ logo
#general
Title
# general
a

ambitious-student-81104

09/15/2021, 11:44 PM
Hi Pants! If my pex binary depends on a file target, in the code I put in this pex binary where should I look for the file? (e.g. the code needs to load a yaml file)
h

hundreds-father-404

09/15/2021, 11:46 PM
Hello! Hm you should have seen a warning when depending on
files()
that it is not included in the pex_binary. Did you see that? Also a warning at https://www.pantsbuild.org/docs/resources#files Instead, you would use
resources()
a

ambitious-student-81104

09/15/2021, 11:46 PM
i added it as a dependency of the pex binary
so the file is there but idk what the path to that file would be
so i have
Copy code
resources(
    name="yaml_files",
    sources=[
        "./*yml",
    ],
)
and then the pex depends on it
looks like i need to
execution_mode="unzip"
?
h

hundreds-father-404

09/15/2021, 11:50 PM
Oh cool, so you are using
resources
. Great. The path should be equivalent to Python code, i.e. with the "source root" stripped. So if the resource is
src/py/project/foo.yaml
, then the resource would become
project/foo.yaml
. You load it with something like
importlib.resources.read_binary("project", "foo.ymal")
https://docs.python.org/3/library/importlib.html#module-importlib.resources
imagine you have the file
src/py/project/subdir/my_util.py
. You'd import it with
import <http://project.subdir.my|project.subdir.my>_util
. For a resource, that would be
importlib.resources.read_binary("project.subdir", "my_util.py")
. Does that make sense?
a

ambitious-student-81104

09/15/2021, 11:53 PM
hmm, is there a way that would make it work with just
open("project/foo.yaml")
?
h

hundreds-father-404

09/15/2021, 11:54 PM
Using the link I sent you, it would be
importlib.resources.open_binary("project", "foo.yaml")
(or
open_text()
).
open()
won't work unfortunately
a

ambitious-student-81104

09/15/2021, 11:54 PM
does "unzip" helps with it at all?
'unzip' mode which also has the benefit of allowing standard use of __file__ and filesystem APIs to access code and resources in the PEX.
h

hundreds-father-404

09/15/2021, 11:54 PM
From the docs:
Filesystem APIs like Python's open() are relative to the current working directory, and they would try to read the files from where the binary is executed, rather than reading from the binary itself.
a

ambitious-student-81104

09/15/2021, 11:55 PM
gotcha..
h

hundreds-father-404

09/15/2021, 11:55 PM
It might actually! I'm not sure, worth trying
a

ambitious-student-81104

09/15/2021, 11:55 PM
yah i'm trying that now
🤞 1
w

witty-crayon-22786

09/15/2021, 11:55 PM
would suggest unzipping the PEX (not that setting, literally unzipping), and looking at where the file is
👍 2
a

ambitious-student-81104

09/15/2021, 11:55 PM
@witty-crayon-22786 that's a good idea, thank you
w

witty-crayon-22786

09/15/2021, 11:55 PM
so on the command line:
unzip -l ${myfile}.pex
1
the “root” of the pex is the root of your pythonpath at runtime
a

ambitious-student-81104

09/15/2021, 11:57 PM
execution_mode="unzip"
worked
🙌 1
6 Views