rushikesh jachak
rushikesh jachak

Reputation: 348

Python Imports doesnt give output in PHP

PHP code

<?php 
echo shell_exec(' python /Users/rushikesh/Sites/hello2.py ');
>?

Python code

print('hello')

import numpy as np

print('hello2')

It outputs only hello, why does the code doesnt give any output after import statements.

I even tried finding error

echo shell_exec(' python /Users/rushikesh/Sites/hello2.py ');

But still am getting blank output after import statements

Upvotes: 1

Views: 242

Answers (2)

Cees Timmerman
Cees Timmerman

Reputation: 19664

Use python -u to skip Python's output buffer, but that still doesn't quite help if Python's library path is incorrect (exit status 3221225477). This works:

PHP start.
<?php 
$cmd = 'c:/python36-32/python -u hello.py';
echo shell_exec($cmd);
?>
PHP end.
print("Hello!")
#import sys; print(sys.path)  # Can we even find numpy?
#help('modules')  # Slow, so use -u flag for Python.
import numpy
print("Imported numpy.")

Upvotes: 0

rushikesh jachak
rushikesh jachak

Reputation: 348

putenv('PYTHONPATH=/Users/rushikesh/anaconda3/lib/python3.6/site-packages:');

$command = escapeshellcmd('/Users/rushikesh/anaconda3/bin/python /Users/rushikesh/Sites/hello2.py');


output = shell_exec($command);

echo $output;

Found from answer of 'call python script *with non-standard library imported* from php'

Upvotes: 1

Related Questions