https://pantsbuild.org/ logo
#pex
Title
# pex
b

bulky-evening-62934

12/17/2020, 5:24 PM
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

witty-crayon-22786

12/17/2020, 5:34 PM
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

bulky-evening-62934

12/17/2020, 5:38 PM
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

witty-crayon-22786

12/17/2020, 5:42 PM
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

bulky-evening-62934

12/17/2020, 5:45 PM
Ok, if I choose unzip, I could have a absolute path for those resource files ?
w

witty-crayon-22786

12/17/2020, 5:46 PM
yes, that’s my understanding
b

bulky-evening-62934

12/17/2020, 5:48 PM
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

hundreds-father-404

12/17/2020, 6:17 PM
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

bulky-evening-62934

12/17/2020, 6:27 PM
That’s definitely an interesting feature, thanks!