正常调用python2.7或通过子进程调用sys.path时的差异

时间:2014-11-14 19:21:18

标签: python-2.7 subprocess python-3.4 sys.path

问题

为什么通过python2.7使用子进程调用时python3呢 没有与正常调用的python2.7相同的sys.path?特别, 通过子进程的python2.7没有"/path/to/site-packages/" sys.path中的目录。

上下文

我想使用fabric部署我写的Django应用。我的问题是 我已在python3中撰写应用,但fabric没有明确python3 支持了。我的解决方法,直到fabricpython3完全兼容, 是使用fab调用subprocess脚本。

出于某种原因,当我通过python2.7使用subprocess致电python3时,我 无法访问site-packages中的任何模块。

python2.7检查

我已经通过Enthought安装了python2.7fabric==1.10.0

$ which python
/Users/.../Library/Enthought/Canopy_32bit/User/bin/python

$ python --version
Python 2.7.6 --  32-bit

$ which fab
/Users/.../Library/Enthought/Canopy_32bit/User/bin/fab

$ fab --version
Fabric 1.10.0
Paramiko 1.15.1

子流程检查

使用子流程从fab内调用python2.7没问题。

$ python
Enthought Canopy Python 2.7.6 | 32-bit | (default, Apr 11 2014, 12:06:39)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.check_output('fab --version', shell=True)
'Fabric 1.10.0\nParamiko 1.15.1\n'

使用子进程在python2.7内调用python3也没问题。

$ python3
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 00:54:21)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.check_output('which python', shell=True)
b'/Users/.../Library/Enthought/Canopy_32bit/User/bin/python\n'
>>> subprocess.check_output('python --version', shell=True)
Python 2.7.6 --  32-bit
b''

DistributionNotFound:Fabric == 1.10.0

然而,即使我的python2.7子进程可以"找到" fab脚本,我 不能打电话。

# python3
>>> subprocess.check_output(['which', 'fab'])
b'/Users/.../Library/Enthought/Canopy_32bit/User/bin/fab\n'
>>> subprocess.check_output(['fab', '--version'])
Traceback (most recent call last):
  File "/Users/.../Library/Enthought/Canopy_32bit/User/bin/fab", line 5, in <module>
    from pkg_resources import load_entry_point
  File "/Applications/Canopy.app/appdata/canopy-1.4.0.1938.macosx-x86/Canopy.app/Contents/lib/python2.7/site-packages/pkg_resources.py", line 2877, in <module>
    working_set.require(__requires__)
  File "/Applications/Canopy.app/appdata/canopy-1.4.0.1938.macosx-x86/Canopy.app/Contents/lib/python2.7/site-packages/pkg_resources.py", line 698, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/Applications/Canopy.app/appdata/canopy-1.4.0.1938.macosx-x86/Canopy.app/Contents/lib/python2.7/site-packages/pkg_resources.py", line 596, in resolve
    raise DistributionNotFound(req)
pkg_resources.DistributionNotFound: Fabric==1.10.0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/subprocess.py", line 620, in check_output
    raise CalledProcessError(retcode, process.args, output=output)
subprocess.CalledProcessError: Command '['fab', '--version']' returned non-zero exit status 1

不在sys.path

中的site-packages

通过python2.7使用子流程调用时,python3似乎可以 没有与正常调用的python2.7相同的sys.path。

正如预期的那样,sys.path没有Enthought "site-packages"目录, 其中包含fabric模块。

# python3
>>> subprocess.check_output('python -c "import sys; print sys.path"', shell=True)
## does not contain '/path/to/Enthought/python2.7/site-packages'

手动将site-packages添加到sys.path

确认可行:当我手动添加正确的时候 "site-packages"目录,我可以成功导入fabric

# python3
>>> subprocess.check_output('python -c\
    "import sys; sys.path.append(\'/path/to/Enthought/site-packages\');\
    from fabric import version; print version.get_version()"',\
    shell = True)
b'1.10.0\n'

其他选择?

必须有更好的方法来确保python2.7,何时 通过python3的子进程调用,具有与python2.7相同的sys.path 正常调用。更熟悉子流程的人可以权衡吗?

其他想法

python2.7能够产生另一个python2.7真的很有趣 通过子进程和该子进程具有正确的site-packages目录 在sys.path。

$ python
>>> import subprocess
>>> subprocess.check_output('python -c "import sys; print sys.path"', shell=True)
## contains "/path/to/Enthought/python2.7/site-packages"

我还比较了python3的sys.path&,python3子程序的python3, 和python3由python2.7子处理,并有点惊讶地发现 这三个结果都是sys.path

1 个答案:

答案 0 :(得分:0)

subprocess支持env参数,如果给出,它将是被调用命令的环境 - 所以复制它,删除任何麻烦的变量,并将该副本传递给{{ 1}}:

subprocess