如何从PHP正确运行Python脚本

I have a PHP script that unzips an archive containing Python scripts into a directory, str_replaces some things inside the script, shell_exec's the script, and then unlinks the script.

I have no problems unzipping the archive or chmodding the contents to 0755. I can execute the generated scripts through terminal and they perform as expected, but with PHP nothing happens!

I have tried specifying absolute paths for Python in my shell_exec and in a shebang within the Python file to no avail. There appear to be no syntax errors or unmet dependencies. I've tried this with all affected directories at chmod 0777 to no avail either. The section of relevant code is as follows...

if (in_array($oldExtension,$allowed)) { 
    list($Width, $Height) = getimagesize($CUD);
    $EXFreshScript = $InstLoc.'/Applications/document-scanner-master.zip';
    $EXTempScript = $InstLoc.'/DATA/'.$UserID.'/TEMPSCRIPTS';
    mkdir($EXTempScript);
    chmod($EXTempScript, 0777);
    $ExtractScripts = shell_exec('unzip '.$EXFreshScript.' -d '.$EXTempScript);
    chmod($EXTempScript.'/document-scanner', 0777);
    $TempScript = $EXTempScript.'/document-scanner/scan.py';
    chmod($EXTempScript.'/document-scanner', 0777);
    $TempScriptGlob = glob($TempScript);
    foreach ($TempScriptGlob as $TSG) {
    chmod($TSG, 0777); }
    chmod($EXTempScript.'/document-scanner/pyimagesearch', 0777);
    $TempScript1 = $EXTempScript.'/document-scanner/pyimagesearch';
    $TempScriptGlob = glob($TempScript1);
    foreach ($TempScriptGlob as $TSG) {
        chmod($TSG, 0777);
    }
    $OutputDoc = $InstLoc.'/DATA/'.$UserID.'/DOCSCANTEMP.jpg';
    $Code = 'DOCSCANTEMP.jpg';
    $newCode = $InstLoc.'/DATA/'.$UserID.'/DOCSCANTEMP.jpg'; 
    $ScriptData = file_get_contents($TempScript);
    $SwapCode = str_replace($Code, $newCode, $ScriptData);
    $WriteCode = file_put_contents($TempScript, $SwapCode);
    $txt = ('OP-Act: Modified the code of '.$TempScript.' with '.$Width.', '.$Height.' on '.$Time.'.');
    $LogFile = file_put_contents($SesLogDir.'/'.$Date.'.txt', $txt.PHP_EOL , FILE_APPEND);
    $txt = ('OP-Act: Executing! '.$TempScript.' on '.$Time.'.');
    $LogFile = file_put_contents($SesLogDir.'/'.$Date.'.txt', $txt.PHP_EOL , FILE_APPEND);
    chmod($TempScript, 0777);
    shell_exec('python '.$TempScript.' -i '.$CTD);
    $txt = ('OP-Act: Execute complete! '.$TempScript.' was executed  on '.$Time.'.');
    $LogFile = file_put_contents($SesLogDir.'/'.$Date.'.txt', $txt.PHP_EOL , FILE_APPEND);
}

If anyone has any ideas I'd greatly appreciate it. The script this came from is over 650 lines long and I've got my issue narrowed down to this. Thanks!

I'll focus on this line:

shell_exec('python '.$TempScript.' -i '.$CTD);
  1. Seems like $CTD is not defined.
  2. Don't use -i flag because it enforces python prompt.
  3. Try to use exec() instead for better debugging.

If it won't help you, let me know what it outputs.

$return = NULL;
$output = [];
exec('python '.$TempScript, $output, $return);
var_dump($return);
var_dump($output);