我正在尝试使用Popen从virtualenv执行python,但是它取自/usr/bin
中的一个(至少是我想的那样,请参见下面的输出):
In [1]: import subprocess
In [2]: proc = subprocess.Popen(['my_program', '-c', 'import sys; print sys.executable; print sys.path;'], executable='/home/user/.pypy/bin/pypy', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
...: print proc.stdout.read(1024)
...:
['', '/usr/lib/pypy/lib_pypy/__extensions__', '/usr/lib/pypy/lib_pypy', '/usr/lib/pypy/lib-python/2.7', '/usr/lib/pypy/lib-python/2.7/lib-tk', '/usr/lib/pypy/lib-python/2.7/plat-linux2', '/usr/local/lib/pypy2.7/dist-packages', '/home/user', '/usr/lib/pypy/dist-packages']
In [3]: proc = subprocess.Popen(['/home/user/.pypy/bin/pypy', '-c', 'import sys; print sys.executable; print sys.path;'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
...: print proc.stdout.read(1024)
...:
/home/user/.pypy/bin/pypy
['', '/home/user/.pypy/lib_pypy/__extensions__', '/home/user/.pypy/lib_pypy', '/home/user/.pypy/lib-python/2.7', '/home/user/.pypy/lib-python/2.7/lib-tk', '/home/user/.pypy/lib-python/2.7/plat-linux2', '/usr/lib/pypy/lib_pypy', '/usr/lib/pypy/lib-python/2.7', '/usr/lib/pypy/lib-python/2.7/plat-linux2', '/usr/lib/pypy/lib-python/2.7/lib-tk', '/home/user/.pypy/site-packages', '/home/user']
您可以看到,没有executable
参数的情况看起来都很好(可执行文件和sys.path都很好。但是,一旦我使用了参数,sys.executable为空字符串,而sys.path是系统的pypy。
我可以在没有executable
参数的情况下运行,只要有另一种方法可以更改进程名称(我无法找到方法)