Viewed   92 times

I am trying to run a Python program using PHP. Here's the code

$command = '/usr/local/bin/python script.py file';
$temp = exec($command, $output);

This works through the command line but not while running it through the browser. I am using Apache so probably it needs the right privileges? I am pretty new to Linux and have no idea how to get this working.

Any help would be appreciated!

Edit 1:

Tried to use proc_open but nothing happens. I gave the full path to the script. Made the script executable but no luck. Any other things I can try on the server? (It's a CentOS 5)

 Answers

3

You need to pass the full path to the script and you also need to make sure that the script is readable by the user running the web server (which means every directory in the path must be +x to the web user).

Monday, November 7, 2022
 
sousdev
 
1

another option, you can set path to interpreter in 1st line of script test.py

#!/usr/local/bin/python2.7 

but you need make test.py executable

chmod +x path_to_file/test.py

and run from php as

exec('path_to_file/test.py');

P.S. be attentive administrators sometimes disable exec function on servers for safety. disable_functions="popen,exec,system,passthru,proc_open,shell_exec" ....

Sunday, November 27, 2022
 
alpha
 
3

Capture the stdout and stderr of the subprocess:

from subprocess import Popen, PIPE

proc = Popen(['/var/www/scripts/resolveId.py', 'arg1', 'arg2'], stdout=PIPE, stderr=PIPE)
stdout, stderr = proc.communicate()
Thursday, December 8, 2022
 
chel
 
1

I'd say you just need to prepend 'call' to your activate.bat invocation, to ensure that the current batch file is resumed after activate is executed:

call %~dp0envScriptsactivate.bat

Consider doing the same for deactivate.bat. Furthermore, if you want to ensure that the current cmd.exe environment is not polluted by a call to your batch file, consider wrapping your commands in a setlocal/endlocal command pair.

Friday, August 19, 2022
1

I finally found a way to do this. If I create a zip file, I must create __main__.py at the root of the zip. Thus, it is possible to launch the script inside the main and call if from bash with the following command :

python myArchive.zip

This command will run the __main__.py file! :)

Then I can create .command file to launch the script with proper parameters.

You can also put some code in the __main__.py file to give you more flexibility if you need to pass arguments for example.

ex: python __main__.py buildProject

The reference documentation is here: https://docs.python.org/2/library/runpy.html

Sunday, September 25, 2022
 
tatsu
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :