I have an application that has some files that nee...
# general
c
I have an application that has some files that need to follow, so I packaged them all in an 'archive'. The files are deployed in the same location as the pex binary. This works fine, and I can also tweak it to be able to run from 'dist/app.pex' by providing tha data directory as a parameter. But it doesn't work well with './pants run', because the current directory is changed to the tmp one and the data dir has to be an absolute path. This makes it a bit cumbersome to debug. Is there a simpler way that I missed?
e
What version of Pants?:
Copy code
$ echo $PWD
/home/jsirois/dev/pantsbuild/jsirois-scie-pants
$ PEX_INTERPRETER=1 dist/scie-pants-linux-x86_64 run tools -- -c 'import os; print(f"I am here: {os.getcwd()}")'
I am here: /home/jsirois/dev/pantsbuild/jsirois-scie-pants
$ dist/scie-pants-linux-x86_64 -V
2.15.0rc0
Yeah, just tried this back through to 2.11.0 and the same in each case, $CWD under
./pants run
is the repo root.
h
Note that you can set
run_goal_use_sandbox=False
on the
pex_binary
target, and then
./pants run
will run directly from the sources in the repo, instead of from sandboxed sources. But whether you do that or not, in either case the CWD is the repo root, not the sandbox root.
But maybe the issue is that the data dir is computed relative to the running code's
__file__
or something?
In that case
run_goal_use_sandbox=False
would fix this
b
Just as another option, are you aware of the option of packaging data directly into the PEX? This can be done via a
resource
instead of a
file
and having the Python files depend on it. The data then has to be read via
importlib.resources
. https://www.pantsbuild.org/docs/assets
e
It doesn't have to actually. All modes of PEX execution involve 1st extracting the PEX to disk, so
__file__
now always works and you can load the data relative to
__file__
if the data is embedded as a resource. Just another option. You still need a Pants
resource
target as Huon suggests either way you read the file.
👍 1
c
Thanks all for the suggestions! ❤️ I did think about using resources, but I wasn't sure if I can use them like that (the files are ansible scripts that will be executed by an external process). If the pex is always unpacked, then it should work if I just get their exact locations. In any case, I think my problem with "pants run" was because I was running the pex binary -- if instead I just run the python module, then it should be working. I will test all the alterntives, to make sure 🙂