2010-06-27 58 views
3

我想用Python编写一些Python包安装脚本到virtualenv中。我写一个函数来安装的virtualenv如何在Python中运行上下文感知命令?

def prepareRadioenv(): 
    if not os.path.exists('radioenv'): 
     print 'Create radioenv' 
     system('easy_install virtualenv') 
     system('virtualenv --no-site-package radioenv') 
    print 'Activate radioenv' 
    system('source radioenv/bin/activate') 

我尝试使用“源radioenv /斌/激活”来激活虚拟环境中,不幸的是,使用os.system创建一个子做的命令。激活所做的环境更改会随着子流程而消失,但不会影响Python进程。问题在于,我如何在Python中执行一些上下文感知命令序列?

又如:

system("cd foo") 
system("./bar") 

在这里,CD好好尝试影响下面的系统( “条”)。如何让这些环境生活在不同的命令中?

有没有类似上下文感知的shell?所以我可以这样写一些Python代码:

shell = ShellContext() 
shell.system("cd bar") 
shell.system("./configure") 
shell.system("make install") 
if os.path.exists('bar'): 
    shell.system("remove") 

谢谢。

回答

3

要在Python中激活virtualenv,请使用execfileactivate_this.py脚本(使用virtualenv创建)。

activate_this = os.path.join("path/to/radioenv", "bin/activate_this.py") 
execfile(activate_this, dict(__file__=activate_this)) 
1

您试图将Python用作shell吗?

在由丹尼尔·罗斯曼,这似乎是你所需要的最重要的部分答案的同时,应注意:

shell.system("cd bar") 

Python中的拼写为:

os.chdir("bar") 

检查os模块可用于您似乎需要的其他功能,如rmdir,removemkdir

相关问题