导入到python时PHP执行(python)不起作用

时间:2017-08-17 18:19:51

标签: php python exec nltk ipc


当尝试使用

从php执行python代码时
echo  shell_exec('python search.py "'.$a.'" "'.$b.'"');

当仅导入sys时,python脚本正在超出$ a和$ b

    #!/usr/bin/env python
    import sys
    sys.stderr = sys.stdout
    a = sys.argv[1]
    b = sys.argv[2] 
    print a+" && "+b

但是当我尝试导入其他内容如Numpy时,php什么都不返回:

    #!/usr/bin/env python
    import sys
    from nltk.corpus import stopwords
    from nltk import word_tokenize
    sys.stderr = sys.stdout
    a = sys.argv[1]
    b = sys.argv[2] 
    print a+" && "+b

我尝试从机器本身(从ssh)运行它,并从它一切正常工作..

已经尝试在stackoverflow和其他地方查看一些问题,但似乎没有什么对我有用..

我正在运行Debian并使用python2.7

我收到了这个错误:

Traceback (most recent call last): File "search.py", line 5, in from nltk.corpus import stopwords File "/usr/local/lib/python2.7/dist-packages/nltk/__init__.py", line 137, in from nltk.stem import * File "/usr/local/lib/python2.7/dist-packages/nltk/stem/__init__.py", line 29, in from nltk.stem.snowball import SnowballStemmer File "/usr/local/lib/python2.7/dist-packages/nltk/stem/snowball.py", line 26, in from nltk.corpus import stopwords File "/usr/local/lib/python2.7/dist-packages/nltk/corpus/__init__.py", line 66, in from nltk.corpus.reader import * File "/usr/local/lib/python2.7/dist-packages/nltk/corpus/reader/__init__.py", line 105, in from nltk.corpus.reader.panlex_lite import * File "/usr/local/lib/python2.7/dist-packages/nltk/corpus/reader/panlex_lite.py", line 15, in import sqlite3 File "/usr/lib/python2.7/sqlite3/__init__.py", line 24, in from dbapi2 import * File "/usr/lib/python2.7/sqlite3/dbapi2.py", line 28, in from _sqlite3 import * ImportError: /usr/lib/python2.7/lib-dynload/_sqlite3.x86_64-linux-gnu.so: failed to map segment from shared object

感谢您的帮助!

解决! 应该在PHP中指定Python的文件夹 并将导入cgi,cgitb添加到Pythonn

1 个答案:

答案 0 :(得分:0)

您需要将整个路径添加到echo命令。类似的东西:

echo  shell_exec('usr/bin/doc/python search.py "'.$a.'" "'.$b.'"');

谢谢,aditya