Running a Python script

  $PathToPython= '/usr/bin/python3';     //Change this if needed to the path of your Python executable 

    $PathToScript = $_SERVER['DOCUMENT_ROOT'];      //Will give: "/var/www/vhosts/photonstreams.com/httpdocs"
    $PathToScript .= "/myfolder/myscript.py";

  //Execute the Python script and capture the output 
  $Output = []; 
  $Status = 0; 
  exec("$PathToPython$PathToScript", $Output, $Status); 
  if ($Status === 0)
  {
    //----- SUCCESS -----
    echo "Output from Python script:<br>";
    foreach ($Output as $line)
    {
      echo htmlspecialchars($line) . "<br>";
    }
  }
  else
  {
    //----- AN ERROR OCCURED -----
    echo "Error executing Python script ($Status).";
  }
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *