Python导入不提供PHP输出

时间:2019-02-18 18:08:26

标签: php python shell

PHP代码

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

Python代码

print('hello')

import numpy as np

print('hello2')

它仅输出问候,为什么代码在import语句后没有给出任何输出。

我什至试图发现错误

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

但是在导入语句之后仍然会得到空白输出

2 个答案:

答案 0 :(得分:1)

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;

根据答案“ call python script *with non-standard library imported* from php”找到

答案 1 :(得分:0)

使用python -u来跳过Python的输出缓冲区,但是如果Python的库路径不正确(退出状态3221225477),那仍然没有多大帮助。这有效:

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.")