How can you obtain the OS's argv[0] (not sys.argv[0]) in Python?
<p>I want to obtain the true value of the operating system's argv[0] in a Python program. Python's sys.argv[0] is not this value: it is the name of the Python script being executed (with some exceptions). What I want is a foo.py that will print "somestring" when executed as</p>

<pre><code>exec -a "somestring" python foo.py
</code></pre>

<p>The trivial program</p>

<pre><code>#! /usr/bin/env python
import sys
print sys.argv[0]
</code></pre>

<p>will print "foo.py" instead.</p>

<p>Does anyone know how to obtain this? There are some related functions in the Python C API: e.g. Py_GetProgramName. But this doesn't seem to be exposed to the Python world anywhere. Py_GetProgramFullPath works off of argv[0] but munges it try to obtain a path to a Python interpreter. (This value is propagated to sys.executable, so that variable isn't right either.) Do I really have to write a C module to get this value?</p>

<p><strong>Edit:</strong> Also asked (but not helpfully answered) <a href="http://bytes.com/topic/python/answers/24340-getting-real-argv-0-python" rel="nofollow" title="here">here</a>.</p>
