0

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

2
  • Does the Python code work when run outside Codeigniter? Commented Feb 7 at 9:12
  • Try using exec() with the second parameter set to see what gets returned from your script. Commented Feb 10 at 8:27

1 Answer 1

1

There are a few points to consider when debugging this:

  1. Bear in mind that the CLI command is executed by your web server, in your case apparently Xampp. It's important to check whether Xampp web server user has access to execute a command on a file. Because executing whoami or echo is different compared to invoking the Python interpreter to run a program. You can read more on this here.

  2. As Simon Weber mentioned, try using exec() with the $output parameter and see if the command output is populated within $output. You can read more about exec() here.

  3. If you can already execute any code in your runPythonScript() function using a browser, that means you have configured the routing properly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.