如何在Python中运行上下文感知命令?

时间:2022-09-13 10:53:52

I want to write some python package installing script in Python into virtualenv. I write a function for installing virtualenv

我想用Python编写一些python包安装脚本到virtualenv中。我写了一个安装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')

I try to use "source radioenv/bin/activate" to activate the virtual environment, unfortunately, os.system create a subprocess for doing the command. The environment change made by activate disappear with the subprocess, it doesn't affect the Python process. Here comes the problem, how can I execute some context-aware command sequence in Python?

我尝试使用“source radioenv / bin / activate”来激活虚拟环境,不幸的是,os.system创建了一个用于执行命令的子进程。由activate生成的环境更改随子进程消失,不会影响Python进程。问题出现了,如何在Python中执行一些上下文感知命令序列?

Another example:

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

Here the cd doens't affect following system(".bar"). How to make those environment context lives in different commands?

这里cd不会影响以下系统(“.bar”)。如何使这些环境上下文存在于不同的命令中?

Is there something like context-aware shell? So that I can write some Python code like this:

是否有像上下文感知的shell?这样我就可以编写一些像这样的Python代码:

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

Thanks.

2 个解决方案

#1


3  

To activate the virtualenv from within Python, use the activate_this.py script (which is created with the virtualenv) with execfile.

要从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))

#2


1  

You are trying to use Python as a shell?

您是否尝试将Python用作shell?

In parallel with the answer by Daniel Roseman, which seems to be the biggest part of what you need, note that:

与丹尼尔罗斯曼的答案并行,这似乎是你需要的最大部分,请注意:

shell.system("cd bar")

is spelt in Python as:

拼写在Python中:

os.chdir("bar")

Check the os module for other functions you seem to need, like rmdir, remove and mkdir.

检查os模块以查找您需要的其他功能,例如rmdir,remove和mkdir。

#1


3  

To activate the virtualenv from within Python, use the activate_this.py script (which is created with the virtualenv) with execfile.

要从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))

#2


1  

You are trying to use Python as a shell?

您是否尝试将Python用作shell?

In parallel with the answer by Daniel Roseman, which seems to be the biggest part of what you need, note that:

与丹尼尔罗斯曼的答案并行,这似乎是你需要的最大部分,请注意:

shell.system("cd bar")

is spelt in Python as:

拼写在Python中:

os.chdir("bar")

Check the os module for other functions you seem to need, like rmdir, remove and mkdir.

检查os模块以查找您需要的其他功能,例如rmdir,remove和mkdir。