Asked  7 Months ago    Answers:  5   Viewed   35 times

Is it possible to run a Python script within PHP and transferring variables from each other ?

I have a class that scraps websites for data in a certain global way. i want to make it go a lot more specific and already have pythons scripts specific to several website.

I am looking for a way to incorporate those inside my class.

Is safe and reliable data transfer between the two even possible ? if so how difficult it is to get something like that going ?



You can generally communicate between languages by using common language formats, and using stdin and stdout to communicate the data.

Example with PHP/Python using a shell argument to send the initial data via JSON


// This is the data you want to pass to Python
$data = array('as', 'df', 'gh');

// Execute the python script with the JSON data
$result = shell_exec('python /path/to/ ' . escapeshellarg(json_encode($data)));

// Decode the result
$resultData = json_decode($result, true);

// This will contain: array('status' => 'Yes!')


import sys, json

# Load the data that PHP sent us
    data = json.loads(sys.argv[1])
    print "ERROR"

# Generate some data to send to PHP
result = {'status': 'Yes!'}

# Send it to stdout (to PHP)
print json.dumps(result)
Wednesday, March 31, 2021
answered 7 Months ago

this python script will have no output and the php code won't save command's output to the variable output instead it will print it immediately, you should use shell_exec for that


$output = shell_exec("python");
print $output;


Tested on Ubuntu Server 10.04. I hope it helps you also on Archlinux.



$command = escapeshellcmd('/usr/custom/');
$output = shell_exec($command);
echo $output;


In Python file '' verify this text in first line: (see shebang explain):

#!/usr/bin/env python

Also Python file should have correct privileges (execution for user www-data / apache if Php script runs in browser or through curl) and/or must be "executable". Also all commands in .py file must have correct privileges.

chmod +x

Source: Running a Python script from PHP

Saturday, May 29, 2021
answered 5 Months ago

No. When a process is killed, the operating system releases all operating system resources (memory, sockets, file handles, …) previously acquired by that process.

Saturday, May 29, 2021
answered 5 Months ago

Update -

Now that I am aware of PHP, the mistake lies in using the single-quotes '. In PHP, single quoted strings are considered literals, PHP does not evaluate the content inside it. However, double quoted " strings are evaluated and would work as you are expecting them to. This is beautifully summarized in this SO answer. In our case,

$output = passthru("python $start_word $end_word");

would work, but the following won't -

$output = passthru('python $start_word $end_word');

Original answer -

I think the mistake lies in

$output = passthru("python $start_word $end_word");

Try this

$output = passthru("python ".$start_word." ".$end_word);
Tuesday, September 21, 2021
answered 1 Month ago

I find it odd that the filePath of C:\wamp\.... would work on a Linux machine. Anyway, have you tried NOT escaping the slashes?

The issue can be related to several things, including OS specific methods of implementing the different program execution functions. I recommend just going through the different options, I always put the command into a single variable and then print out the variable so that I can see what I am about to run, if you saw two slashes, then you might know that there is no need to try to escape the slashes, however without printing it to the screen you wouldn't know.

Try going through the other process execution functions to see what works for you.

The one that I have found works MOST like running it at the command line, is the backtick operator, and then next is system

try something like this.

$pyscript = 'C:\wamp\www\testing\scripts\';
$python = 'C:\Python27\python.exe';
$filePath = 'C:\wamp\www\testing\uploads\thumbs\10-05-2012-523.jpeg'

$cmd = "$python $pyscript $filePath";
echo $cmd;

$pyscript = '';
$python = 'C:Python27python.exe';
$filePath = 'C:wampwwwtestinguploadsthumbs10-05-2012-523.jpeg'

$cmd = "$python $pyscript $filePath";
echo $cmd;

EDIT: AHHH! I just figured it out, you are surrounding your command with single ticks '' which means the variable inside are not being replaced. Use double quotes instead. That WILL solve the problem, but I still recommend echoing the command out before you run it so that you would see the command you are running and you would be able to identify an issue like this.

Tuesday, September 28, 2021
The Coding Wombat
answered 4 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :