0

I have a python script that I want to execute from my codeigniter controller. I found a similar question but wasn't quite able to see the out put (Executing a Python file from Codeigniter)

Is there a way that I can run a python script from my CI appliation and get the output into a variable or something like that ?

1 Answer 1

1

You can execute a python script from php for example with the passthru function.

<?php
ob_start();
passthru('/full/path/to/python /full/path/to/test.py arg1 arg2');
$output = ob_get_clean(); 

echo $output;
?>

Or with the "system" function.

<?php
system('full/path/to/python /full/path/to/test.py', $return_value);
echo $return_value;
?>

But your python script should return something if you want to "see" someting.

import sys
 #calculate stuff
sys.stdout.write('Bugs: 5 |Other: 10\n')
sys.stdout.flush()
sys.exit(0)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response @Jakob. "return something" is a print statement good enough ?
If you use "system" in php and "print" in python the "system" function will echo / print it in your php script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.