os.execve
仮想環境でいくつかの悪ふざけを試しています。sys.executable
現在のpythonプロセスを別のpythonサブプロセスに置き換えると、空になるという問題が発生します。
以下の例は、何が起こっているかを示しています (これを python シェル内で実行します):
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
Pythonシェルで上記のコマンドを実行した私の完全な出力:
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.
>>> import os, sys
>>> print(sys.executable) # works this time
/usr/bin/python
>>> 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.
>>> import sys # yes, again
>>> print(sys.executable) # is empty
>>>
sys.executable
空であることは私に問題を引き起こしています。最も顕著なのは空であるplatform.libc_ver()
ために失敗することsys.executable
です:
>>> import platform
>>> platform.libc_ver()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/platform.py", line 163, in libc_ver
f = open(executable,'rb')
IOError: [Errno 21] Is a directory: '/tmp'
上記の例は、呼び出し後に実行されたことに注意してくださいos.execve(...)