0

I have two files in my "py" file in the "www" in wamp (local server) the first file is python and the code is:

print("Hello")
print("Jane")
print("doe")

the script in php:

<!DOCTYPE>
<html>

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>php with py</title>
</head>

<body>
<h1>Hi</h1>
<?php

$output = array();
exec("py C:\wamp64\www\py\py_8.py", $output);
var_dump( $output);
?>

</body>

</html>

it doesn't work, I get this error:

C:\wamp64\www\py\py_8.php:19:
array (size=0)
  empty

I have checked in cmd that line:

py C:\wamp64\www\py\py_8.py

and it works!

what is wrong?

8
  • Is your safe_mode on? Commented May 18, 2017 at 15:11
  • @Carlos Alexandre how do I check? Commented May 18, 2017 at 15:12
  • @napuzba answered well, check there first. Commented May 18, 2017 at 15:13
  • Check with phpinfo if the safe mode is active php.net/manual/pt_BR/function.phpinfo.php Commented May 18, 2017 at 15:17
  • removed safe mode and still the same. Commented May 18, 2017 at 15:32

2 Answers 2

1

You should use full path to python.

exec('"full-path-to-python" "C:\wamp64\www\py\py_8.py"', $output);
Sign up to request clarification or add additional context in comments.

3 Comments

Try to enclose with the paths with ".
didn't understand.. I tried this: exec("<C:\Users\User\AppData\Local\Programs\Python> C:\wamp64\www\py\py_8.py", $output);
First, remove the <> . Also, verify that you python is also x64 (using python -v)
1

run python --help in the command line, you can find the python path in the output

python --help

usage: C:\Users\Roofe\AppData\Local\Programs\Python\Python35\python.exe [option] ... [-c cmd | -m mod | file | -] [arg] ... Options and arguments (and corresponding environment variables): -b : issue warnings about str(bytes_instance), str(bytearray_instance)

then set the python path in you php exec like,

exec("C:\Users\Roofe\AppData\Local\Programs\Python\Python35\python.exe C:\wamp64\www\py\py_8.py", $output);

4 Comments

got this: Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\User>PS C:\Users\Roofe> python --help 'PS' is not recognized as an internal or external command, operable program or batch file.
in the cmd window, run python --help, and first you have python installed.
Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\User>python --help 'python' is not recognized as an internal or external command, operable program or batch file. and I have python!
check where your python is installed, and check the environment path

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.