8

I wanted to know how to run python script using php code. I have tried different options like

 $output = exec("python  /var/GAAutomationScript.py");

 $command = escapeshellcmd('/var/GAAutomationScript.py');
 $output = shell_exec($command);

But unable to run the python script. My application is in Laravel. Is it possible to run python script using Laravel scheduler jobs e.g. using artisan commands?

3
  • Is the python script executable by the account running the web server? Commented Aug 2, 2016 at 15:16
  • This subject is answered very detailed here: stackoverflow.com/questions/41020068/… Commented Jan 30, 2019 at 10:22
  • Do you get any error ? Commented Feb 1, 2019 at 13:23

2 Answers 2

3

Some things to mention about your question :

  • I will highly recommend use symfony/process instead of shell_exec directly. It is already available in laravel. You can check easily if your command is successful or not and get standard output in string format etc
  • When you run a shell script from a web app, the user permissions matter. For example, the laravel web user is www-data and your python script is lets say ubuntu.nbody for example, it will then check what permissions the python script has. Are users other than ubuntu in this example are allowed to run it.
  • Then if you think giving 777 permissions to python script will get rid of the permission problem then thats a risk too.
  • Last but not the least, create a laravel console command, add the snippet which would run python script from symfony/process. Then run it and debug the errors. once it works then you can add that as in laravel's cron jon.

See this to know mo about scheduling artisan commands.

You can also avoid creating artisan command and add shell directly, as it internally uses symfony/process. See this for that reference

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

3 Comments

Hi, I got an error about a module not found when I am trying to import an external extension inside my python code even though the python code is runnable via terminal. Do you have any solutions?
@Forrest Are you sure that you are using exact same command which works on local in your laravel app? Can you give some more info so it is easy to check the error?
Hi, my problem is solved. I actually have to import it with an absolute path. Thanks
1

You can try this for executing the Python script using Laravel's Process class (Laravel uses Process class from Symfony's Process component):

<?php

use Illuminate\Support\Facades\Process;
use Illuminate\Process\Exceptions\ProcessFailedException;

public function testPythonScript()
{
    $process = new Process(['python3', '/path/to/your_script.py', 'arg1', 'arg2']);
    $result = $process->run();
    
    if (!$result->successful()) {
        throw new ProcessFailedException($process);
    }

    $data = $result->output();

    dd($data);
}

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.