Perl: executing a python script from perl program

ambuli used Ask the Experts™
Hi Experts,

I need to somehow use a python script in a perl program.  I don't know python, so I cannot write the entire thing in python.  But, I want to use a python script that was written by someone else.

The python script accepts one input.

How can I do it?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You would call it the same way you would from the command line just using the system function:
$Python = 'C:\\Python24\\python.exe';
$PyScript = '';
$Args = "arg1";
system("$Python $PyScript", $Args) == 0 or die "Could not execute script.";

Open in new window

Top Expert 2009

Or, similar to brutaldev's suggestion, call it with backticks to read the output of the Python script.

my $s = ` foo bar`;
print "Script returned $s";


Thank you very much.  One small question.  The system call does not take $inputfile as an argument to the script. Also, I had to remove the " " around $Python $PyScript.
What could be wrong.

foreach $inputfile (@inputfiles)
    system($Python $PyScript, $inputfile ) == 0 or die "Could not execute script.";
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.


If the argument is filenameone, it should be passed as it is. Not "filenameone".
Top Expert 2009
You are missing quotes:

You have:
  system($Python $PyScript, $inputfile )   <-- wrong

But should have something like:

  system($Python, $PyScript, $inputfile)

Though probably this will work if your Python interpreter has a file association for that extension.

  system($PyScript, $inputfile)


Thank you.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial