Python sys.executable is empty
<p>I am testing out doing some shenanigans with <code>os.execve</code> and virtual environments. I am running into the problem where <code>sys.executable</code> is empty if I replace the current python process with another python subprocess.</p>

<p>The example below shows what's going on (run this inside a python shell):</p>

<pre><code>import os, sys
print(sys.executable) # works this time
os.execve("/usr/bin/python", [], {}) # drops me into a new python shell
import sys # yes, again
print(sys.executable) # is empty
</code></pre>

<p>The full output of me running the commands above in a python shell:</p>

<pre><code> lptp [ tmp ]: python
Python 2.7.10 (default, Oct 14 2015, 16:09:02) 
[GCC 5.2.1 20151010] on linux2
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import os, sys
&gt;&gt;&gt; print(sys.executable) # works this time
/usr/bin/python
&gt;&gt;&gt; os.execve("/usr/bin/python", [], {}) # drops me into a new python shell
Python 2.7.10 (default, Oct 14 2015, 16:09:02) 
[GCC 5.2.1 20151010] on linux2
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import sys # yes, again
&gt;&gt;&gt; print(sys.executable) # is empty

&gt;&gt;&gt;
</code></pre>

<p><code>sys.executable</code> being empty is causing me problems, most notably that <code>platform.libc_ver()</code> fails because <code>sys.executable</code> is empty:</p>

<pre><code>&gt;&gt;&gt; import platform
&gt;&gt;&gt; platform.libc_ver()
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "/usr/lib/python2.7/platform.py", line 163, in libc_ver
    f = open(executable,'rb')
IOError: [Errno 21] Is a directory: '/tmp'
</code></pre>

<p>Note that the example above was run after calling <code>os.execve(...)</code></p>

<p>Python relies on <code>argv[0]</code> and several environment variables to determine <code>sys.executable</code>. When you pass an empty argv and environment, Python doesn't know how to determine its path. At the very least, you should provide <code>argv[0]</code>:</p>

<pre><code>os.execve('/usr/bin/python', ['/usr/bin/python'], {})
</code></pre>

python execve