How to run a command inside virtual environment using Python
<p>I have the virutalenv created and installed. I have also installed jsnapy tool inside my virutal env. </p>

<p>This is the script that we are using:</p>

<pre><code>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
</code></pre>

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

<p>[EDIT1]</p>

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

<pre><code>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
</code></pre>

<p>Each call to <code>os.system()</code> 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 <code>os.system()</code></p>

<p><strong>run.sh</strong></p>

<pre><code>source ~/TestAutomation/End2EndAutomation/bin/activate
End2EndAutomation/bin/jsnapy
deactivate
</code></pre>

<p><strong>Python</strong></p>

<pre><code>os.system('source run.sh')
</code></pre>

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

python