Python sys.executable 为空
Python sys.executable is empty
我正在测试用 os.execve
和虚拟环境做一些恶作剧。如果我将当前 python 进程替换为另一个 python 子进程,我将 运行 解决 sys.executable
为空的问题。
下面的示例显示了正在发生的事情(运行 this inside a 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'], {})
我正在测试用 os.execve
和虚拟环境做一些恶作剧。如果我将当前 python 进程替换为另一个 python 子进程,我将 运行 解决 sys.executable
为空的问题。
下面的示例显示了正在发生的事情(运行 this inside a 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'], {})