executing python script from php under windows

I find it odd that the filePath of C:\\wamp\\.... would work on a Linux machine. Anyway, have you tried NOT escaping the slashes?

The issue can be related to several things, including OS specific methods of implementing the different program execution functions. I recommend just going through the different options, I always put the command into a single variable and then print out the variable so that I can see what I am about to run, if you saw two slashes, then you might know that there is no need to try to escape the slashes, however without printing it to the screen you wouldn't know.

Try going through the other process execution functions to see what works for you. http://www.php.net/manual/en/ref.exec.php

The one that I have found works MOST like running it at the command line, is the backtick operator, and then next is system

try something like this.

$pyscript = 'C:\\wamp\\www\\testing\\scripts\\imageHandle.py';
$python = 'C:\\Python27\\python.exe';
$filePath = 'C:\\wamp\\www\\testing\\uploads\\thumbs\\10-05-2012-523.jpeg'

$cmd = "$python $pyscript $filePath";
echo $cmd;
`$cmd`


$pyscript = 'C:\wamp\www\testing\scripts\imageHandle.py';
$python = 'C:\Python27\python.exe';
$filePath = 'C:\wamp\www\testing\uploads\thumbs\10-05-2012-523.jpeg'

$cmd = "$python $pyscript $filePath";
echo $cmd;
`$cmd` 

EDIT: AHHH! I just figured it out, you are surrounding your command with single ticks '' which means the variable inside are not being replaced. Use double quotes instead. That WILL solve the problem, but I still recommend echoing the command out before you run it so that you would see the command you are running and you would be able to identify an issue like this.


This is a very old thread, but still comes up among the first results in Google, so it is worth an update.

In 2020, Under Python 3.8 and Windows 10 the right solution is simply:

<?
$output = shell_exec('C:\path\to\pythonscript.py');
print ($output);
?>

Everything else will result in an error. Took me a good few hours to figure out.