2

I have a need to execute a Python script from a Laravel Command, but I can't find any methods in Illuminate\Console\Command to complete my task.

I want to run this command via the console:

C:\Python34\python H:\myapp\app\python\questionPopulator.py

To do so, I tried the following in my commands fire() method:

public function fire()
{
    $this->call('C:\Python34\python H:\myapp\app\python\questionPopulator.py');
}


public function fire()
{
    $this->line('C:\Python34\python H:\myapp\app\python\questionPopulator.py');
}

None of them work, as Laravel is expecting me to be calling another Laravel command from them. What is the best way to call a simple python script via a Laravel Command?

2 Answers 2

2

try PHP exec function: http://php.net/manual/en/function.exec.php

exec('C:\Python34\python H:\myapp\app\python\questionPopulator.py');
Sign up to request clarification or add additional context in comments.

Comments

1

Well you got a few options

1) system

  system('C:\Python34\python H:\myapp\app\python\questionPopulator.py');

2) exec

  exec('C:\Python34\python H:\myapp\app\python\questionPopulator.py');

3) shell_exec - my favorite

  $result = shell_exec('C:\Python34\python H:\myapp\app\python\questionPopulator.py');

shell_exec runs the script but returns a string.

You could encode the result in a json format.

This way the 2 ecosystems are kept separately.

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.