Hey, I have a question for `Pex` , I add some reso...
# pex
b
Hey, I have a question for
Pex
, I add some resource files into the built Pex file, and for some reasons, I need to know their absolute path when running the pex file, how could I get it ? is it under $PEX_ROOT ? by
pkg_resources
?
w
you’d want to use
pkg_resources
unless the PEX has been unzipped, the file will not exist loose on disk: instead, it will be loaded from within the PEX file
and
pkg_resources
APIs support doing that transparently
b
I tried
resources.resource_filename()
, but it doesn’t work,
Copy code
NotImplementedError: resource_filename() only supported for .egg, not .zip
For example, I have a file
libtest/app/data/style.css
in Pex file, I failed to run
resources.resource_filename("<http://libtest.app|libtest.app>", "data/style.css")
But I could get the content by
resources.resource_string("<http://libtest.app|libtest.app>", "data/style.css")
w
exactly
as the error message mentions, it doesn’t have a filename (on disk) when it is inside a zip
but you can load it using the
resource_string
etc APIs
BUT
if you build the pex with
Copy code
--unzip, --no-unzip
                        Whether or not the pex file should be unzipped before
                        executing it. If the pex file will be run multiple
                        times under a stable runtime PEX_ROOT the unzipping
                        will only be performed once and subsequent runs will
                        enjoy lower startup latency. [Default: do not unzip.]
… then it should automatically extract itself before running.
b
Ok, if I choose unzip, I could have a absolute path for those resource files ?
w
yes, that’s my understanding
b
another way might be just passing those resource files along with pex file, it’s less elegant but must work … Thanks for the help ; )
h
another way might be just passing those resource files along with pex file, it’s less elegant but must work …
This is why Pants’s
archive
type exists: https://www.pantsbuild.org/docs/resources#archive-create-a-zip-or-tar-file. It may give some inspiration here
(Also, we’ve continued to make lots of improvements to Pants since y’all last evaluated using it. It may be worth taking another look. Let us know if we can help!)
👍 1
b
That’s definitely an interesting feature, thanks!