2013-10-30 69 views
1

我尝试安装过程中setup.py调用一个Python脚本,使用自定义安装:为什么从setup.py调用Python脚本调用Python shell?

class CustomInstall(install): 
    def run(self): 
     install.run(self) 

     ... 

     p = subprocess.Popen(
      [sys.executable, 'demo_package/deploy_database.py'], 
      shell=True, 
      stdout=subprocess.PIPE, 
      cwd=os.path.join(self.install_lib, 'demo_package')) 

     out, err = p.communicate() 

setup(..., cmdclass=dict(install=CustomInstall)) 

当Ubuntu的机器,工艺上部署,而不是执行deploy_database.py包,说明不了什么。当我用Ctrl + C手动停止,输出似乎表明,而不是实际运行deploy_database.py,它只是简单的Python:

^CDownloading/unpacking PypiPackagesMonitoring 
    Downloading demo-1.0.64.zip 
    Running setup.py egg_info for package demo 

Installing collected packages: demo 
    Running setup.py install for demo 

    Python 3.3.2+ (default, Oct 9 2013, 14:50:09) 
    [GCC 4.8.1 on linux 
    Type "help", "copyright", "credits" or "license" for more information. 
Cleaning up... 
Operation cancelled by the user 
Storing complete log in /home/.../.pip/pip.log 

有什么错我叫Python脚本的方式吗?我应该怎么做呢?

+0

或者你可以使用'execfile'。 –

+0

'shell = True' *应该*暗示您*需要*一个*不能被表达为一个简单的字符串序列的特性,即当使用'shell = True'时,您必须使用一个表示整个字符串的字符串* shell命令*(不*仅仅调用外部程序,但包含一些shell特性,如管道/重定向/调用内置等)。否则,你应该使用'shell = False',你可以用'sys.argv'的相同格式来表示要执行的程序和它的参数。混合这两种方法会导致这种奇怪的结果。 – Bakuriu

回答

1

shell=True[sys.executable, 'demo_package/deploy_database.py']不需要,并导致问题。你这样做的方式(如果你省略shell=True)是首选方式,因为它绕过了shell。

shell=True使Popen将传递的命令移交给shell(例如/bin/bash,取决于为当前用户配置的shell)。

在Unix与壳=真[...]如果ARGS是:只有在执行列表中的传递的第一命令,该列表的其它元素被作为参数传递给壳本身(从docs)传递序列中,第一项指定命令字符串,任何其他项目将被视为shell本身的附加参数。

shell=True有点危险,但在某些情况下也有用。详情请参阅the documentation