How to run Python 3 Script in Laravel 7?

  4766 views   3 months ago Laravel Python

Hi, Artisan

In this article i will share with you how to run or execute python 3 script in your laravel application. some people ask me why you need to run python 3 scripts in laravel? why you don't write same logic in laravel? so, i have also answer for that people. 

Why i need this functionality in Laravel ?

Recently i have been work one laravel application. our laravel application also working fine and no need to any extra requirement in it. but recently we want to add some functionalilty in laravel application. we want to add image recognition in our application as you all know this functionality not built easy with laravel script.

When you need this requirement ?

As you all know python do any things. here is some common examples.

  • 1.) image recognition
  • 2.) when you have billian of records and download in CSV
  • 3.) check image blur or not
  • 4.) etc....

Write simple Python Script

first, we need to write very simple python script for testing.

# User inputs the string and it gets stored in variable str
str = input("Enter a string: ")

# counter variable to count the character in a string
counter = 0
for s in str:
      counter = counter+1
print("Length of the input string is:", counter)

 Run Python Code in Laravel

laravel already provie a Process class for run the script in laravel. so, we also use this class and will run our python script in the laravel application.

public function testPythonScript()
    $process = new Process("python3 /var/www/laravel/myLaravelApp/app/PythonScript/");
    if (!$process->isSuccessful()) {
        throw new ProcessFailedException($process);

    $data = $process->getOutput();


i hope you like this article. in next article i will share with you how to export 100M record with help of python and laravel.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on

Related Articles