I am new to Python and trying to use subprocess for running a script inside another script. I have found several resources online that were very close but unfortunately couldn't help me run my code without error yet.
Here is what I want to do: In my script1 (main script), I am creating a fabfile.py (script2). This script2 or fabfile.py needs to be executed from script1. After some research, I figured out that execfile and os.systems are not good options therefore I decided to use subprocess. (Reference: How can I make one python file run another?)
Here is what I am doing but not working:
from os.path import expanduser
home = expanduser("~")
import os
os.getcwd()
desk = "/Desktop"
path = str(home)+str(desk)
f = open("fabfile.py","w") # Creating a fabfile.py
f.write("from fabric.api import run \ndef task1(): \n run('ls')")
import subprocess
host = raw_input("Enter the host IP with username e.g. root@10.0.0.2:")
p1 = subprocess.Popen(['fab', '-f path/fabfile.py', '-H host'],stdout=subprocess.PIPE)
output = p1.communicate()
print output
Note: In the line
p1 = subprocess.Popen(['fab', '-f path/fabfile.py', '-H host'],stdout=subprocess.PIPE)
I have tried MANY different formats - quote and double quotes placement, $
and %
for variable etc etc. but none seem to be working. Any idea what am I doing wrong?
The examples I see are usually simple with no variables being used as arguments.