Hi Pants! If my pex binary depends on a file targe...
# general
a
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
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
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
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
hmm, is there a way that would make it work with just
open("project/foo.yaml")
?
h
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
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
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
gotcha..
h
It might actually! I'm not sure, worth trying
a
yah i'm trying that now
🤞 1
w
would suggest unzipping the PEX (not that setting, literally unzipping), and looking at where the file is
👍 2
a
@witty-crayon-22786 that's a good idea, thank you
w
so on the command line:
unzip -l ${myfile}.pex
1
the “root” of the pex is the root of your pythonpath at runtime
a
execution_mode="unzip"
worked
🙌 1