使用subprocess.call()运行bin / buildout失败

时间:2012-12-20 22:12:19

标签: python buildout

tl,博士:我的shell可以很好地使用“bin / buildout”,但是当我从Python运行subprocess.call([“bin / buildout”])或类似的东西时,它会失败。为什么?任何解决方法?

我添加了'python'标签而不仅仅是'buildout',因为这可能是使用subprocess.call()或os.system()从shell调用python脚本与python的一个细微差别。我不知道他们为什么会有所不同。它可能是Buildout的东西,因为Buildout会重写自己,然后重新启动。

为了构建我想要展示的示例,我从一个新的Ubuntu 12.04 LTS虚拟框开始。然后我在它上面安装git(sudo apt-get install git)并克隆我们的一个几乎没有任何内容的存储库:

git clone git://github.com/lizardsystem/lizard-datasourceviewer.git

然后我进入它并运行bootstrap.py:

cd lizard-datasourceviewer
python bootstrap.py

到目前为止,这么好。现在可以运行“bin / buildout”,它将运行没有问题(好吧,在某些时候它出错,因为系统没有matplotlib - 这是预期的结果)。但是不要这样做,因为一旦你这样做,下面的错误就不会发生。如果您这样做,请删除该目录并再次克隆它。

如果INSTEAD我从Python运行它,比如:

$ python
>>> import subprocess
>>> subprocess.call(["bin/buildout"])

然后这更早失败(见下面的错误)。这是一个问题,因为我们想从脚本调用buildout。子进程调用的变化,如os.system(“bin / buildout”)或subprocess.call([“/ bin / sh”,“ - c”,“bin / buildout”])没有帮助。 一旦bin / buildout从命令行运行一次,问题就消失了,即使再次调用bootstrap.py也是如此。

我知道什么时候出错了。最初,bin / buildout看起来像这样:

#!/usr/bin/python

import sys
sys.path[0:0] = [
  '/usr/lib/python2.7/dist-packages',
  '/usr/lib/python2.7/dist-packages',
  '/usr/lib/python2.7/dist-packages',
  '/usr/lib/python2.7/dist-packages',
  '/usr/lib/python2.7/dist-packages',
  '/usr/lib/python2.7/dist-packages',
  '/usr/lib/python2.7/dist-packages',
  '/usr/lib/python2.7/dist-packages',
  '/usr/lib/python2.7/dist-packages',
  '/usr/lib/python2.7/dist-packages',
  '/usr/lib/python2.7/dist-packages',
  '/home/vagrant/lizard-datasourceviewer/eggs/zc.buildout-1.4.4-py2.7.egg',
  ]

import zc.buildout.buildout

if __name__ == '__main__':
    zc.buildout.buildout.main()

从命令行运行bin / buildout后,它会“分发”,重写bin / buildout,然后自行重启。结果bin / buildout看起来像这样:

#!/usr/bin/python

import sys
sys.path[0:0] = [
  '/home/vagrant/lizard-datasourceviewer/eggs/zc.buildout-1.4.4-py2.7.egg',
  '/home/vagrant/lizard-datasourceviewer/eggs/distribute-0.6.27-py2.7.egg',
  ]

import zc.buildout.buildout

if __name__ == '__main__':
    zc.buildout.buildout.main()

当从Python而不是从shell运行bin / buildout时,这个重写和重启步骤似乎失败了。错误消息是:

vagrant@precise64:~/lizard-datasourceviewer$ python
Python 2.7.3 (default, Apr 20 2012, 22:39:59) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.call(["bin/buildout"])
Getting distribution for 'mr.developer==1.21'.
Got mr.developer 1.21.
Getting distribution for 'buildout-versions==1.5'.
Got buildout-versions 1.5.
mr.developer: Creating missing sources dir /home/vagrant/lizard-datasourceviewer/src.
Getting distribution for 'distribute==0.6.27'.
Before install bootstrap.
Scanning installed packages
Setuptools installation detected at /usr/lib/python2.7/dist-packages
Non-egg installation
Removing elements out of the way...
Already patched.
/usr/lib/python2.7/dist-packages/setuptools.egg-info already patched.
After install bootstrap.
Don't have permissions to write /usr/local/lib/python2.7/dist-packages/setuptools-0.6c11-py2.7.egg-info, skipping
Creating /usr/local/lib/python2.7/dist-packages/setuptools-0.6c11-py2.7.egg-info
**error: /usr/local/lib/python2.7/dist-packages/setuptools-0.6c11-py2.7.egg-info: Permission denied**
An error occured when trying to install distribute 0.6.27. Look above this message for any errors that were output by easy_install.
While:
  Installing.
  Checking for upgrades.
  Getting distribution for 'distribute==0.6.27'.
Error: Couldn't install: distribute 0.6.27
1

正如您所看到的,它正在尝试安装到此用户无权访问的系统dist-packages中。但为什么?从shell运行相同的脚本有什么区别?

2 个答案:

答案 0 :(得分:2)

不确定原因,但使用subprocess.call(['/bin/bash', '-c', 'bin/buildout'])似乎为我解决了问题,其中subprocess.call(['/bin/sh', '-c', 'bin/buildout'])失败了。 Ubuntu 12.04。

希望它有所帮助。 :)

答案 1 :(得分:0)

尝试使用可选参数subprocess.call()调用shell=True

来自subprocess documentation

  

如果shell为True,则将通过执行指定的命令   贝壳。如果您主要使用Python,这可能很有用   增强的控制流程,它提供了大多数系统外壳,仍然需要   方便地访问其他shell功能,如shell管道,   文件名通配符,环境变量扩展和扩展〜   到用户的主目录。

相关问题