我想写一些python包,在virtualenv中安装Python脚本。我编写了一个用于安装virtualenv的函数
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')我尝试使用"source radioenv/bin/ activate“来激活虚拟环境,不幸的是,os.system创建了一个子进程来执行该命令。activate所做的环境更改随子进程一起消失,不会影响Python进程。问题来了,我如何在Python中执行一些上下文感知的命令序列?
另一个例子:
system("cd foo")
system("./bar")在这里,cd不影响以下系统(“.bar”)。如何让这些环境上下文存在于不同的命令中?
有没有类似于上下文感知shell的东西?这样我就可以像这样写一些Python代码:
shell = ShellContext()
shell.system("cd bar")
shell.system("./configure")
shell.system("make install")
if os.path.exists('bar'):
shell.system("remove")谢谢。
发布于 2010-06-28 03:14:18
要从Python中激活virtualenv,可以在execfile中使用activate_this.py脚本(用virtualenv创建)。
activate_this = os.path.join("path/to/radioenv", "bin/activate_this.py")
execfile(activate_this, dict(__file__=activate_this))发布于 2010-06-28 04:30:50
您正在尝试使用Python作为shell吗?
与Daniel Roseman的答案类似,这似乎是你需要的最大部分,请注意:
shell.system("cd bar")在Python中拼写为:
os.chdir("bar")在os模块中查看您似乎需要的其他函数,如rmdir、remove和mkdir。
https://stackoverflow.com/questions/3128452
复制相似问题