'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



Solution 1:[1]

As of Pex 2.1.53 (https://github.com/pantsbuild/pex/blob/main/CHANGES.rst#2153) at runtime a PEX exposes the original PEX file location in the "PEX" environment variable.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 John Sirois