ambitious-student-81104
09/15/2021, 11:44 PMhundreds-father-404
09/15/2021, 11:46 PMfiles()
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()
ambitious-student-81104
09/15/2021, 11:46 PMambitious-student-81104
09/15/2021, 11:47 PMambitious-student-81104
09/15/2021, 11:48 PMresources(
name="yaml_files",
sources=[
"./*yml",
],
)
and then the pex depends on itambitious-student-81104
09/15/2021, 11:49 PMexecution_mode="unzip"
?hundreds-father-404
09/15/2021, 11:50 PMresources
. 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.resourceshundreds-father-404
09/15/2021, 11:51 PMsrc/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?ambitious-student-81104
09/15/2021, 11:53 PMopen("project/foo.yaml")
?hundreds-father-404
09/15/2021, 11:54 PMimportlib.resources.open_binary("project", "foo.yaml")
(or open_text()
). open()
won't work unfortunatelyambitious-student-81104
09/15/2021, 11:54 PMambitious-student-81104
09/15/2021, 11:54 PM'unzip' mode which also has the benefit of allowing standard use of __file__ and filesystem APIs to access code and resources in the PEX.
hundreds-father-404
09/15/2021, 11:54 PMFilesystem 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.
ambitious-student-81104
09/15/2021, 11:55 PMhundreds-father-404
09/15/2021, 11:55 PMambitious-student-81104
09/15/2021, 11:55 PMwitty-crayon-22786
09/15/2021, 11:55 PMambitious-student-81104
09/15/2021, 11:55 PMwitty-crayon-22786
09/15/2021, 11:55 PMunzip -l ${myfile}.pex
witty-crayon-22786
09/15/2021, 11:56 PMambitious-student-81104
09/15/2021, 11:57 PMexecution_mode="unzip"
worked