Alexis
Alexis

Reputation: 33

How to resolve Python Pex binary file location

Using the pex tool to package python proyects (https://pex.readthedocs.io/en/v2.1.55/); is there a way to know where the original Pex file is located?. Until now I just have been able to see where the "unzipped" files are located but need to programmatically resolve the original file location since the program can run under different environments. I have a test script ~/print_location.py

import pathlib

print(pathlib.Path(__file__))

And the pex binary ~/print_location.pex

Output:/home/usr/.pex/installed_wheels/4d...e5/print_location-1-py3-none-any.whl/print_location.py

Expected:/home/usr/print_location.pex

Upvotes: 1

Views: 769

Answers (2)

John Sirois
John Sirois

Reputation: 481

As of Pex 2.1.53 at runtime a PEX exposes the original PEX file location in the PEX environment variable.

The docs say:

If your code benefits from knowing whether it is running from within a PEX or not, you can inspect the PEX environment variable. If it is set, it will be the absolute path of the PEX your code is running in. Normally this will be a PEX zip file, but it could be a directory path if the PEX was built with a --layout of packed or loose.

Upvotes: 2

bwang
bwang

Reputation: 94

sys.argv[0] should be able to get you what you want.

documentation

Upvotes: -1

Related Questions