1

I have the virutalenv created and installed. I have also installed jsnapy tool inside my virutal env.

This is the script that we are using:

Filename : venv.py 
import os
os.system('/bin/bash  --rcfile ~/TestAutomation/End2EndAutomation/bin/activate')
os.system('End2EndAutomation/bin/jsnapy')

ubuntu@server:~/TestAutomation$ python venv.py
(End2EndAutomation) ubuntu@sdno-server:~/TestAutomation$ ^C

We need to know, is how we can get into virutalenv, run a command and deactivate it using python script?

[EDIT1]

i used the code given in the comment. its just entering virutal env. When i issue exit, its running jsnapy command.

ubuntu@server:~/TestAutomation$ python venv.py
(End2EndAutomation) ubuntu@server:~/TestAutomation$ exit
exit
usage:
This tool enables you to capture and audit runtime environment of
networked devices running the Junos operating system (Junos OS)

Tool to capture snapshots and compare them
It supports four subcommands:
 --snap, --check, --snapcheck, --diff
1. Take snapshot:
        jsnapy --snap pre_snapfile -f main_configfil

2 Answers 2

1

Each call to os.system() will create a new bash instance and terminate the previous one. To run all the commands in one bash instance you could put all your commands inside a single bash script and call that from os.system()

run.sh

source ~/TestAutomation/End2EndAutomation/bin/activate
End2EndAutomation/bin/jsnapy
deactivate

Python

os.system('source run.sh')

Alternatively, you could write a multiline bash command, as long as it's all in one os.system() call.

Sign up to request clarification or add additional context in comments.

2 Comments

its not invoking jnapy until i issue exit from virtual environment . Is there any efficient way of doing it rather doing a system call.
you need to add more information about issues like that to your question.
1

Two successive calls to os.system() will create two independent processes, one after the other. The second will run when the first finishes. Any effects of commands executed in the first process will have been forgotten and flushed when the second runs.

You want to run the activation and the command which needs to be run in the virtualenv in the same process, i.e. the same single shell instance.

To do that, you can use bash -c '...' to run a sequence of commands. See below.

However, a better solution is to simply activate the virtual environment from within Python itself.

p = os.path.expanduser('~/TestAutomation/End2EndAutomation/bin/activate_this.py')
execfile(p, dict(__file__=p))
subprocess.check_call(['./End2EndAutomation/bin/jsnapy'])

For completeness, here is the Bash solution, with comments.

import subprocess
subprocess.check_call(['bash', '-c', """
    . ~/TestAutomation/End2EndAutomation/bin/activate
    ./End2EndAutomation/bin/jsnapy"""])

The preference for subprocess over os.system is recommended even in the os.system documentation.

There is no need to explicitly deactivate; when the bash command finishes, that will implicitly also deactivate the virtual environment.

The --rcfile trick is a nice idea, but it doesn't work when the shell you are calling isn't interactive.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.