简体   繁体   中英

How to run python script with Laravel's command fire method?

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?

尝试使用PHP exec函数: http//php.net/manual/en/function.exec.php

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM