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');
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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
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?
Please or to participate in this conversation.