2012-12-20 38 views
3

tl,dr:我的buildouts可以在shell中使用「bin/buildout」,但在運行subprocess.call時失敗([「bin/buildout」]] )或類似的Python。爲什麼?任何解決方法?使用subprocess.call()運行bin/buildout失敗

我已經加入了「蟒蛇」的標籤,而不是僅僅「擴建」,因爲這可能是在使用subprocess.call調用從外殼python腳本VS蟒蛇一個微妙的差異()或使用os.system()。我不知道他們爲什麼會有所不同。這可能是一個建設性的事情,因爲建設重寫自己然後重新啓動自己。

爲了構建我想展示的例子,我從一個新的Ubuntu 12.04 LTS虛擬框開始。然後,我在其上安裝的git(命令和apt-get安裝GIT)和克隆我們的資料庫中的一個已經在它幾乎沒有什麼:

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

然後我進入這個目錄並運行bootstrap.py:

cd lizard-datasourceviewer 
python bootstrap.py 

到目前爲止,這麼好。現在可以運行「bin/buildout」,並且它可以毫無麻煩地運行(當然,由於系統沒有matplotlib,這是預期的結果)。但不要這樣做,因爲一旦你這樣做,下面的錯誤將不會發生。如果你這樣做,刪除目錄並再次克隆。

如果是相反,我從Python中運行,如:

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

那麼這個失敗早得多(見下文)中的錯誤。這是一個問題,因爲我們想從腳本調用buildout。子進程調用的變體,如os.system(「bin/buildout」)或subprocess.call([「/ bin/sh」,「-c」,「bin/buildout」))不起作用。 一旦bin/buildout從命令行運行一次,問題就會消失,即使再次調用bootstrap.py。

我知道什麼時候出問題了。最初,斌/擴建看起來是這樣的:

#!/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() 

而且在命令行中運行斌/擴建後,它就會被「分配」,改寫斌/擴建,並重新啓動。結果斌/擴建看起來是這樣的:

#!/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運行失敗。錯誤信息如下:

[email protected]:~/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

不知道爲什麼,但使用subprocess.call(['/bin/bash', '-c', 'bin/buildout'])似乎爲我解決它,其中subprocess.call(['/bin/sh', '-c', 'bin/buildout'])失敗。 Ubuntu 12.04。

希望它有幫助。 :)

+0

然後設置'shell = True',它會使用你的默認shell。 –

+1

@MartijnPieters nope,除了Windows以外的任何其他操作'subprocess.call(shell_code,shell = True)'與傳遞'subprocess.call(['/ bin/sh','-c',shell_code]')相同。 '。它不會爲您的用戶使用默認的登錄shell。 – monk

+0

有趣;因此'distribute'安裝需要'bash'。 –

0

嘗試使用可選參數shell=True調用subprocess.call()

subprocess documentation

如果殼爲True,則指定命令將通過 殼執行。如果您主要將Python用於其在大多數系統shell中提供的增強控制流,並且仍然希望 可以方便地訪問其他shell功能,如shell管道, 文件名通配符,環境變量擴展和〜 的擴展到用戶的主目錄。

+0

我試過這個(subprocess.call([「bin/buildout」],shell = True)),但它沒有幫助。同樣的錯誤。 – RemcoGerlich