How to run context sensitive commands in Python?

I want to write a python package installing a script in Python in virtualenv. I am writing a function to install 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 am trying to use "source radioenv / bin / activate" to activate a virtual environment, unfortunately os.system creates a subprocess to execute the command. The environment change made by activation disappears with the subprocess, this does not affect the Python process. This raises the problem, how can I execute some context-sensitive sequence of commands in Python?

Another example:

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

Here cd does not affect the next system (". Bar"). How to make this environment context live in different teams?

Is there something like a context sensitive shell? So I can write Python code like this:

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

Thank.

+3
2

virtualenv Python, activate_this.py script ( virtualenv) execfile.

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

Python ?

, , , , , , :

shell.system("cd bar")

Python :

os.chdir("bar")

os , , rmdir, remove mkdir.

+1

Source: https://habr.com/ru/post/1751992/


All Articles