0

I have a Perl script that I need to run with Python, and I've been trying to use subprocess to do it, unsuccessfully. I'm able to get the command to run just fine on the command line, but subprocess isn't able to get it to work.

If I have a Perl script like this:

#!/usr/bin/perl

use strict;
use warnings;

my $name = shift;

print "Hello $name!\n";

I am able to successfully run the command on the command line like so

C:\current\path> perl test.pl world
>>>Hello world!

But when I try and invoke the same command with subprocess, I get this error

cmd = 'perl test.pl world'
pipe = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE)
>>>"'perl' is not recognized as an internal or external command, operable program or batch file."

I've tried different combinations of creating cmd as a list

cmd = ['perl','test.pl','world']

and of giving subprocess the absolute path to Perl

cmd = ['C:\path\to\perl.exe','test.pl','world']

but nothing gets it to work. I'm able to get subprocess to play well without Perl

pipe = subprocess.Popen('dir',shell=True,stdout=subprocess.PIPE)
pipe.stdout.read()
>>>Volume in drive C is Windows7_OS....

I'm absolutely sure that Perl is in my PATH, because like I said I can invoke the command just fine in the command line. I've looked around at different posts that suggested to check os.environ["COMSPEC"], adding Perl to the subprocess env, and nothing has worked.

EDIT: I also can't get the command to work with any other subprocess methods: check_output() returns an empty byte string, and call() returns 1.

Any sort of fix or alternative solution would be immensely appreciated. I'm running Python 3.4 on Windows 7 64bit. I've tried 32- and 64-bit Python as well, to no avail.


UPDATE: I've been able to get this to work on Mac, but with one difference: I can't have shell=True. Other than that, any subprocess function I want will work, including Popen, check_output, and call.

So I guess this is a Windows problem more than anything. I've tried not setting shell=True on my Windows machine, but I always get this error:

WindowsError: [Error 193] %1 is not a valid Win32 application

UPDATE 2: I've also tried creating a .bat file that runs the command, but I get the same error as well I try to just call Perl.

pipe = subprocess.Popen('test.bat',shell=True,stdout=subprocess.PIPE)
>>>"'test.bat' is not recognized as an internal or external command, operable program or batch file."

Where test.bat has only one line:

echo Hello, World
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
mr.plow
  • 43
  • 7

3 Answers3

0

you could try using os.system('command') instead. Apparently its not well regarded for some reason, or at least not as well regarded as subprocess though. Alternately, you could try some of the other subprocess methods, such as call, check_call, or check_output. I've had a similar problem with Popen in the past and switching to one of these methods helped.

bgenchel
  • 3,739
  • 4
  • 19
  • 28
  • I probably should have mentioned that none of the subprocess methods are working with Perl either. check_output returns an empty byte string and call always returns 1. I'll give os.system() a try though. – mr.plow Aug 15 '14 at 16:29
  • Looks like os.system(cmd) isn't cutting it either. Still returning an exit code of 1 :/ – mr.plow Aug 15 '14 at 16:40
0

I got the same error while trying to run a perl script from Python.

I managed to get it work by adding the full path of the perl.exe (with an extra escape character, as '\') to the command I was trying to run as below:

pipe = subprocess.Popen('C:\\Perl64\\bin\\perl myscrip.pl',shell=True,stdout=subprocess.PIPE)
pipe.stdout.read()
ykorkmaz
  • 15
  • 4
0

This works fine for me. I know it's been a while and things may have just fixed themselves.

I'm using activestate perl 5.8.9
and python 3.9.5 on Windows 10.

from subprocess import Popen, PIPE

proc = Popen(['perl', 'test.pl', 'world'], stdout=PIPE, stderr=PIPE)
stdout, stderr = proc.communicate()

if proc.returncode == 0:
    print(stdout.decode())
else:
    print(stderr.decode())
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61