Skip to content

How to pass variables to a Python script via Php

Sometimes you may be in need of passing variables to a Python script that is being executed by Php as command line. To do this follow the below steps

Here is our Python script which just prints whatever variable that’s passed to it

#!/usr/local/bin/python3
import nltk
import sys
input=sys.argv[1]
print (input)

Here is our Php script which executes the Python script

//Getting input variable from another script
<?php
$input=$_GET['input'];
$command = escapeshellcmd('python3 a2.py '.$input);
$output = exec($command);
echo $output;
?>

The above Php scripts passes the input variable from Php to Python and prints out the same