I'm new to codeigniter 4 and I try to simply run a python code from file in controller method:
So I have following:
public function runPythonScript(){
log_message('debug', 'OKKKKrun1' );
//echo shell_exec("whoami"); //by this i got: nt authority\system
$scriptPath = "C:/xampp/htdocs/ci4-test/public/assets/python/welcome.py";
//$command = "echo Hello World 2>&1";//this works properly
$command = escapeshellcmd("C:/Users/L/AppData/Local/Microsoft/WindowsApps/python.exe $scriptPath 2>&1");//returns empty and no log in python itself so does not exacuted at all
$output = shell_exec($command);
log_message('info', 'OKKKKrun2' .print_r($output,true));//this logged empty
if ($output === null) {
$error = error_get_last();
log_message('error', 'Error executing script: ' . print_r($error, true));//this logged empty
echo "An error occurred. Check the logs.";
} else {
echo 'result is: '.$output;
}
}
While the path is added to system variables and shell_exec is worked properly with inline comment as mentioned above, the welcome python code does not execute at all; and any logging does not performed there; accordingly the result of python code run is empty and I get nothing when I run code on browser other than bellow:
INFO - 2025-02-05 12:41:47 --> OKKKKrun1
INFO - 2025-02-05 12:41:47 --> Python script output:
INFO - 2025-02-05 12:41:47 --> Executing command: C:/Users/L/AppData/Local/Microsoft/WindowsApps/python.exe C:/xampp/htdocs/ci4-test/public/assets/python/welcome.py 2^>^&1
INFO - 2025-02-05 12:41:47 --> OKKKKrun2
ERROR - 2025-02-05 12:41:47 --> Error executing script:
Here is my python code:
import sys
try:
print("Welcome to CodeIgniter!")
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
and part in routs.php
$routes->get('run-script', 'Properties\PropertiesController::runPythonScript');
Can anyone describe what the is problem and how I should resolve it?
Thanks in advance
Run python script in controller method codeigniter4