简体   繁体   English

Python sys.executable为空

[英]Python sys.executable is empty

I am testing out doing some shenanigans with os.execve and virtual environments. 我正在测试使用os.execve和虚拟环境进行一些os.execve I am running into the problem where sys.executable is empty if I replace the current python process with another python subprocess. 如果将当前的python进程替换为另一个python子进程, sys.executable为空的问题。

The example below shows what's going on (run this inside a python shell): 以下示例显示了发生的情况(在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

The full output of me running the commands above in a python shell: 我在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 being empty is causing me problems, most notably that platform.libc_ver() fails because 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'

Note that the example above was run after calling os.execve(...) 请注意,上面的示例是在调用os.execve(...)之后运行的

Python relies on argv[0] and several environment variables to determine sys.executable . Python依靠argv[0]和几个环境变量来确定sys.executable When you pass an empty argv and environment, Python doesn't know how to determine its path. 当您传递一个空的argv和环境时,Python不知道如何确定其路径。 At the very least, you should provide argv[0] : 至少,您应该提供argv[0]

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM