Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Simone94's avatar

Execute python script from laravel

I should execute a script in python from a view in Laravel after pressing a button. In the controller I put the code to execute the script and that is:

public function scan() {

    $process = new Process(['C:\Simone\University\Smart IoT Devices\Lab_Raspy\Bluetooth', 'prova.py']);

    $process->run();

    if (!$process->isSuccessful()) { throw new ProcessFailedException($process); }

    return redirect()->route('device');

}

but when I press the button in the view I get this error:

The command "'C:\Simone\University\Smart IoT Devices\Lab_Raspy\Bluetooth' 'prova.py'" failed. Exit Code: 127(Command not found) Working directory: /var/www/html/public Output: ================ Error Output: ================ sh: 1: exec: C:\Simone\University\Smart IoT Devices\Lab_Raspy\Bluetooth: not found

The view is set as follows:

@extends('backend.layouts.app')

@section('content')

@csrf

<button type="submit" class="btn btn-ghost-primary" id="scanner">Scan Device</button><br><br>
<tr>

  <th scope="col">ID</th>

  <th scope="col">USERNAME</th>

  <th scope="col">MAC ADDRESS</th>

</tr>
@foreach ($data as $item)
<tr>
  <th scope="row">{{$item->id}}</th>
    <td>{{$item->USERNAME}}</td>
    <td>{{$item->MAC_ADDR}}</td>
  <td>
    <a href="/admin/singleDevice/{{ $item->id }}" class="btn btn-primary">Select</a>
  </td>
</tr>
@endforeach

@endsection

I also tryied with the command

$process = shell_exec('python C:\Simone\Università\Smart IoT Devices\Lab_Raspy\Bluetooth\prova.py 2>&1');

return redirect()->route('backend.auth.user.device');

And the error change in:

Symfony\Component\Routing\Exception\RouteNotFoundException Route [backend.auth.user.device] not defined.

And my route is:

Route::get('device' ,[DeviceController::class, 'index'])->name('devices');

Route::get('dict', [DeviceController::class, 'visualizeData'])->name('dict');

Route::get('singleDevice/{device}', [DeviceController::class, 'singleDev'])->name('singleDev');

Route::get('scan', [DeviceController::class, 'scan'])->name('scan');

Where am I going wrong?

0 likes
32 replies
Sinnbeck's avatar

First of try using this if you wan to use Process

$process = Process::fromShellCommandline('python C:\Simone\Università\Smart IoT Devices\Lab_Raspy\Bluetooth\prova.py');
Sinnbeck's avatar

@Simone94 That line in itself does a redirect? That is very strange! So if you remove the $process code, you get redirected to the proper page?

Simone94's avatar

@Sinnbeck If I use your line of code it redirects me to that URL I wrote you, if I use my line of code it tells me it can't find the route

Simone94's avatar

@Sinnbeck this is the function where I try to call the script

public function scan()
{
    $process = Process::fromShellCommandline('python C:\Simone\Università\Smart IoT Devices\Lab_Raspy\Bluetooth\prova.py');
    
    return redirect()->route('backend.auth.user.device');
}

This is the routes:

Route::get('device' ,[DeviceController::class, 'index'])->name('devices');
Route::get('dict', [DeviceController::class, 'visualizeData'])->name('dict');
Route::get('singleDevice/{device}', [DeviceController::class, 'singleDev'])->name('singleDev');
Route::get('scan', [DeviceController::class, 'scan'])->name('scan');
Sinnbeck's avatar

@Simone94 You still need to run the command

public function scan()
{
    $process = Process::fromShellCommandline('python C:\Simone\Università\Smart IoT Devices\Lab_Raspy\Bluetooth\prova.py');
  $progress->run(); //run the command
    
    return redirect()->route('backend.auth.user.device');
}
Simone94's avatar

@Sinnbeck yes sorry, I forgot it. However I get this error: Route [backend.auth.user.device] not defined. A clarification, in my form in the view I use the GET method as in the route, is this correct or should I use the POST method?

Sinnbeck's avatar

@Simone94 That is up to you. You can always change it. Personally I would probably use POST

Can you show the backend.auth.user.device route?

Sinnbeck's avatar

@Simone94 That is a view, not a route. You posted several above, but none with that name

Did you mean to redirect to devices ?

   return redirect()->route('devices');
Simone94's avatar

@Sinnbeck sorry, the route is this:

Route::get('scan', [DeviceController::class, 'scan'])->name('scan');

Sinnbeck's avatar

@Simone94 Good start. Now add back the error handling

if (!$process->isSuccessful()) { 
    throw new ProcessFailedException($process); 
}
Sinnbeck's avatar

@Simone94 How do you know if it works? Does the command return something or does it change something on a seperate system?

Simone94's avatar

@Sinnbeck I usually use PyCharm to run the script, currently I don't see any changes on PyCharm. I expect to see the run icon light up like it does when I manually run the script

Sinnbeck's avatar

@Simone94 Not sure it does if you run it via php. Instead you could make it write to a log file or similar so you can see if it was called correctly.

Simone94's avatar

@Sinnbeck I also see it another way. The script should send me some data on a DB that I visualize with the views and this doesn't happen.

Simone94's avatar

@Sinnbeck another clarification, if I put the POST method on the route and the view form, I get the error:

The command "python C:\Simone\Università\Smart IoT Devices\Lab_Raspy\Bluetooth\prova.py" failed. Exit Code: 127(Command not found) Working directory: /var/www/html/public Output: ================ Error Output: ================ sh: 1: python: not found

Sinnbeck's avatar

@Simone94 The full path is something like (check if the path is correct before trying)

C:\Users\Simone\AppData\Local\Programs\Python\Python39.exe
Sinnbeck's avatar

@Simone94 you don't have python installed? Sorry if the path is wrong. I don't have a windows computer, so I can't check it

Simone94's avatar

@Sinnbeck i did it but i had to use python3 and this is the result: "/usr/bin/python3"

Sinnbeck's avatar

@Simone94 How is php run? Inside a virtual machine or something? That is a linux path

Simone94's avatar

@Sinnbeck the script is inside the raspberry that i can see thanks to Pycharm that i have connected to the raspberry...so i have a "virtual" folder on my PC while the real one is on the raspberry

Please or to participate in this conversation.