9

I am trying to run a Perl script from within Python, but I get no output in stdout(), while my script perfectly works when I run it from a shell.

First, here is how I execute it from shell (assume I am on the right directory):

./vmlinkedclone.pl --server 192.168.20.2 --username root --password root 
--vmbase_id 2 --vm_destination_id 41 --vmname_destination "clone-41-snapname" --snapname Snapname

#=> True, []
#=> or False, and a description of the error here 
#=> or an argument error

And here is how I try to call it from Python:

cmd = ['/home/user/workspace/vmlinkedclone.pl', '--server', '192.168.20.2', '--username', 'root', '--password', 'root' ,'--vmbase_id', '2', '--vm_destination_id', '41', '--vmname_destination', 'clone-41-snapname', '--snapname', 'Snapname']
pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
result = pipe.stdout.read()

print "Result : ",result
#=> Result :

Why do I get the desired output when I run my script from Shell, and get nothing from Python ?

1 Answers1

9

Can you just try:

pipe = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

Edit

I did find some issues related to the encoding sometime back and I resolved it in the following manner:

import subprocess
cmd = ['/home/user/workspace/vmlinkedclone.pl', '--server', '192.168.20.2', '--username', 'root', '--password', 'root' ,'--vmbase_id', '2', '--vm_destination_id', '41', '--vmname_destination', 'clone-41-snapname', '--snapname', 'Snapname']
pipe = subprocess.Popen(cmd, shell = True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = pipe.communicate()
result = out.decode()
print "Result : ",result 
Alex
  • 8,093
  • 6
  • 49
  • 79
Pulimon
  • 1,776
  • 4
  • 31
  • 45
  • I still have an empty output :/ –  Nov 19 '12 at 10:20
  • I have just updated the answer. Can you just check that as well? – Pulimon Nov 19 '12 at 10:24
  • 3
    you might also try to print the error as well to see whether any error was encountered or not : print "Error",err – Pulimon Nov 19 '12 at 10:27
  • 1
    Oh it did help me a lot, by priting the stderr, now I know that the script cannot find one of my Perl module, which explains the problem. Thanks o/ –  Nov 19 '12 at 10:30