2

I'm trying to run python script via laravel using symfony process but when i run it I get this error: 'Python' is not recognized as an internal or external command, operable program or batch file. I tried to run the python script with cmd and it run and return the result

my laravel code is this:

`

$text="I luv my <3 iphone & you're awsm apple. DisplayIsAwesome, sooo happppppy  http://www.apple.com";
$process=new Process("Python C:\Users\Bcc\PycharmProjects\twitter-data\Car.py \"{$text}\"");
$process->run();
if (!$process->isSuccessful()){
            throw new ProcessFailedException($process);
}
var_dump($process->getOutput());
die();

` I run this command on cmd: Python C:\Users\Bcc\PycharmProjects\twitter-data\Car.py "I luv my <3 iphone & you're awsm apple. DisplayIsAwesome, sooo happppppy http://www.apple.com"

EDIT: Problem Solved.I move python script to controllers folder and use shell_exec ,so just replace that code with this

    $text="I luv my &lt;3 iphone &amp; you're awsm apple. DisplayIsAwesome, sooo happppppy  http://www.apple.com";
    $result = shell_exec("python ". app_path()."\Http\Controllers\Car.py " . escapeshellarg($text));

    var_dump($result);
    die();
  • If you run the command manually over the command line, what happens? – Jonathon Apr 11 '18 at 08:06
  • 1
    it run and give me the result of the script – Mohammad Mustafa Apr 11 '18 at 08:08
  • How are you running Laravel? Is it through a virtual machine such as Homestead (vagrant)? – Jonathon Apr 11 '18 at 08:14
  • no I run it on my localhost on port 8000 using php storm terminal – Mohammad Mustafa Apr 11 '18 at 08:28
  • Can you post the command you're executing to run the above code? – Jonathon Apr 11 '18 at 08:56
  • sure ,I edit the post with the cmd command – Mohammad Mustafa Apr 11 '18 at 09:03
  • Sorry, I meant the command that you use to run Laravel – Jonathon Apr 11 '18 at 09:29
  • oh sorry ,that's it my command to run laravel php artisan serv – Mohammad Mustafa Apr 11 '18 at 09:42
  • Okay, what is the path to your project's `public` folder on your filesystem? You could try changing your code to use a relative path to your Python script. For example, if your Laravel project is in `C:\Users\Bcc\PHPStormProjects\YourProject/public` you could change the path in your code to be something like... `../../../PycharmProjects/twitter-data/Car.py`. – Jonathon Apr 11 '18 at 09:56
  • i try but it's the same problem – Mohammad Mustafa Apr 11 '18 at 10:11
  • How is this a python question exactly ? Your problem is not with Python itself but with your system not being able to find the Python interpreter, so you could have the very same problem with just any executable. – bruno desthuilliers Apr 11 '18 at 10:24
  • @Jonathon the problem is not with the script path but with the OP's system not finding the python _executable_. – bruno desthuilliers Apr 11 '18 at 10:24
  • @ bruno okay but why on cmd it return the result ? – Mohammad Mustafa Apr 11 '18 at 10:26
  • @MohammadMustafa: I'm not a Windows expert by any mean (last Windows version I worked with was NT4 xD) but I stronly suspect your Python interpreter is not installed system-wide but only locally, so running the command manually _from the right working directory_ works but would fail from another working directory. Anyway: your problem comes from something in your python install / system config / environnement variables / user permissions / whatever other configuration issue that is specific to your own system. – bruno desthuilliers Apr 11 '18 at 10:31
  • @bruno thank you ,i will try to solve it – Mohammad Mustafa Apr 11 '18 at 10:40
  • @brunodesthuilliers Yeah, you're right - my last suggestion of changing to a relative path is definitely wrong as per the error message OP is seeting. I think it's likely an issue with the environment in which the OP is running his Laravel application in that it is not able to see the python interpreter. I suspect that if the OP was running through a WAMP environment locally, his code would probably work. – Jonathon Apr 11 '18 at 11:53
  • I edit the post because i solve the problem using this [link](https://stackoverflow.com/questions/41020068/running-python-script-in-laravel) with some tries ,thanks @Jonathon – Mohammad Mustafa Apr 11 '18 at 22:28

0 Answers0