Fix python is not recognized as an internal or external command when running in Laravel

So, sometimes when coding, we need to finish some tasks using different environment. For example, call php script in python, call python in php, etc.

in this case, I faced problem when calling python script in my Laravel 9 appilcation. This is the code and the error




So, here isi the solution

1. Install Process from Symfony

composer require symfony/process  

2. Next. call them in your Laravel 

use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
3. Lastly, update your code like this

$process = new Process(
            ['python', public_path() . '/python/pdftoimg.py'],
            null,
            ['SYSTEMROOT' => getenv('SYSTEMROOT'), 'PATH' => getenv("PATH")]
        );

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

I hope this will help you. And this is the threat that helped my fix this problem --> Link