3

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.

Community
  • 1
  • 1
user3806770
  • 385
  • 1
  • 6
  • 14

4 Answers4

4
  1. Don't specify python variables in string
  2. Separate flags

    p1 = subprocess.Popen(['fab', '-f', path+'/fabfile.py', '-H', host],stdout=subprocess.PIPE)
    
  3. When joining paths, its a better idea to use os.path.join()

    fab_file = os.path.join(os.path.expanduser("~"), "Desktop", 'fabfile.py')
    
glglgl
  • 89,107
  • 13
  • 149
  • 217
Ashoka Lella
  • 6,631
  • 1
  • 30
  • 39
0

I figured out that there is one more issue, that is, inputing password. Because even after correcting variable issue, I am getting error (a different one). I think instead of using fab, I can simply do,

from subprocess import Popen, PIPE 
host = raw_input("Enter the host IP with user e.g. root@10.0.0.2:") 
conn1 = Popen(['ssh',host,'ls'], stdin=PIPE) 
conn1.communicate('password') 

Reference: Use subprocess to send a password

Community
  • 1
  • 1
user3806770
  • 385
  • 1
  • 6
  • 14
0

Code might not be 100% correct, but something like this should work

import fabric.api as api
from path.my-other-fabfile import my_ssh_connection_blah

api.ls()
host = raw_input("Enter the host IP with username e.g. root@10.0.0.2:")

env['host']=host
my_ssh_connection_blah()
doesnt_matter
  • 121
  • 5
  • 13
-1

Python won't interpolate the variables path and host for you, you need to do it explicitly.

Change:

p1 = subprocess.Popen(['fab', '-f path/fabfile.py', '-H host'],stdout=subprocess.PIPE)

to:

p1 = subprocess.Popen(['fab', '-f ' + path + '/fabfile.py', '-H ' + host],stdout=subprocess.PIPE)
jaynp
  • 3,275
  • 4
  • 30
  • 43