Python Forum
Why is PYTHONEXECUTABLE only supported on MAC OS X?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is PYTHONEXECUTABLE only supported on MAC OS X?
#1
I need a way to set argv[0] when using python's -m option.

The python manual documents the following environment variable:
Quote:PYTHONEXECUTABLE:  If this environment variable is set, sys.argv[0] will be set to its value instead of the value got through the C runtime. Only works on Mac OS X.
This is exactly the functionality that I need but for Linux. Using sys.executable is not a solution since it needs to be referenced in each script module rather than in one common location.

Let me describe my use case and explain why I need this functionality:

Our tool is comprised of:
  • a primary application delivered with an embedded Python interpreter (and an associated library of python code)
  • a collection of python utilities which share some of the same python code
  • a standalone python distribution which matches the version embedded in the primary application.

The python utilities are shipped as symlinks to a common wrapper script in the bin directory. The wrapper is thus able to find the correct python distribution and run the tool itself which resides in a libexec directory. (This gives us a fair measure of platform independence as we completely control the two 32-bit and two 64-bit python distributions.)

For example, consider the following file structure:

tool/bin/.wrapper
tool/bin/tool-util-one@ -> .wrapper
tool/bin/tool-util-two@ -> .wrapper
tool/libexec/util_one.py
tool/libexec/util_two.py

The wrapper heuristically determines that tool-util-one has its code in util_one.py and so forth. Once it finds the correct python and module, it runs the utility using:
os.execve([python, '-m', 'util_one', sys.argv[1:]], env)
As one would expect, argv[0] becomes the python program itself. But then, when python processes the -m option, it changes argv[0] to "path/to/libexec/util_one.py" (as described in https://docs.python.org/3/using/cmdline.html):
Quote:If this option is given, the first element of sys.argv will be the full path to the module file
This is quite undesirable because it breaks the behaviour of code which wants to use sys.argv[0]. This includes, for example, argparse as well as our logging and metrics facilities.

A workaround is to modify every tool to use sys.executable instead of sys.argv but this is ugly and prone to error. It would be much simpler and safer to be able to make a single change in the wrapper. The PYTHONEXECUTABLE environment variable looks to fit the bill perfectly but does not work on Linux.

I have two questions:
  1. Why is this only supported on Mac OS X?
  2. Is there another solution that I can use?
Thank you very much,
Keith
Reply


Messages In This Thread
Why is PYTHONEXECUTABLE only supported on MAC OS X? - by keith_hanlan - Nov-15-2021, 09:05 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020