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

import nltk
import sys
print (input)

Here is our Php script which executes the Python script

//Getting input variable from another script
$command = escapeshellcmd('python3 '.$input);
$output = exec($command);
echo $output;

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