Execute python in a php script using shell_exec()

前端 未结 6 1018
自闭症患者
自闭症患者 2021-01-02 21:45

I\'m experiencing a weird problem in trying to execute python in a php server (LAMP). (safe_mode off)

if I type:

$output = shell_exec(\"ls -lah\");
e         


        
相关标签:
6条回答
  • 2021-01-02 21:55

    What does

    which python
    

    tell you, both from the command line and from shell_exec()? It should tell you which (if any) Python interpreter it's finding (from $PATH). Don't forget that it's quite possible that the $PATH used from the Linux command line might not be the same as the $PATH used by shell_exec()! Once you find the Python interpreter you want to use, you might have to hard code it in the shell_exec().

    0 讨论(0)
  • 2021-01-02 21:56

    I think you need to refer to the full path for your python.

    for example use this instead:

    $output = shell_exec("/usr/bin/python full_path/my_script.py")
    

    instead of:

    $output = shell_exec("python my_script.py");
    
    0 讨论(0)
  • 2021-01-02 22:09

    If you are trying to run the python script using the following code

    $output = shell_exec("python my_script.py");
    

    you will need to use absolute path for my_script.py and give all permissions (I am not sure which ones are sufficient) for the python file.

    0 讨论(0)
  • 2021-01-02 22:11

    I think this may help...

    looks like the output for the python call needs to be routed properly. I was able to make this work within my index.php file for returning the python version...

    shell_exec("python -V 2>&1");
    

    Here is where I found the answer.

    0 讨论(0)
  • 2021-01-02 22:12

    I think kernel not able to find the path for python where it is installed..if you can do echo $PATH..it will show all the paths where to be search a command if given add your python part there and then it may work or you can give absolute path(other than /usr/bin/) see if it works..I need to test it too.

    0 讨论(0)
  • 2021-01-02 22:14

    Most likely the web server doesn't have appropriate rights to execute shell commands. To fix this, run the 'sudo visudo' command and add the following line to the sudoers file:

    www-data ALL=NOPASSWD: ALL

    Also, make sure that the /var/www directory belongs to the www-data user and group (use sudo chown -R www-data:www-data /var/www to set the correct owner). The details are here http://www.raspberry-pi-geek.com/Archive/2014/07/PHP-on-Raspberry-Pi

    Also refer Can't execute python script from php

    0 讨论(0)
提交回复
热议问题