簡體   English   中英

Python sys.executable為空

[英]Python sys.executable is empty

我正在測試使用os.execve和虛擬環境進行一些os.execve 如果將當前的python進程替換為另一個python子進程, sys.executable為空的問題。

以下示例顯示了發生的情況(在python shell中運行):

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 shell中運行以上命令的完整輸出:

 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(...)之后運行的

Python依靠argv[0]和幾個環境變量來確定sys.executable 當您傳遞一個空的argv和環境時,Python不知道如何確定其路徑。 至少,您應該提供argv[0]

os.execve('/usr/bin/python', ['/usr/bin/python'], {})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM