从命令行运行脚本与使用 PHP 的 exec() 运行脚本有什么区别?
我正在尝试使用 PHP 中的 exec() 运行 Python 脚本。当我直接使用 cmd
窗口运行我的命令时,它工作正常,但当我从 PHP 中的 exec()
运行它时,它会产生错误。
我的 Python 脚本使用 NTLK 来查找专有名词。示例命令:
"C:\Python25\python.exe" "C:\wamp\projects\python\trunk\tests\find_proper_nouns.py" "I went to London this morning"
当我从 cmd
运行它时,返回 [London]
,但当我从 exec()< 运行相同的命令时,会在 Apache 日志中引发错误/code>. 该脚本肯定运行正常 - 如果我将 python 脚本更改为
print "Hello World"
则返回正常。
我知道对于任何人来说,知道如何修复这个 NLTK 错误都是一个很大的要求,但我确实可以通过任何指示来解释为什么从 exec
运行它与 cmd
不同。 (命令是相同的)。
我在 Windows 7 上使用 Apache 2.2.11 运行 WAMP。
这是 Apache 日志中的错误:
Traceback (most recent call last):
File "C:\wamp\projects\python\trunk\tests\find_proper_nouns_command_line.py", line 6, in <module>
parts = nltk.pos_tag(text)
File "C:\Python25\lib\site-packages\nltk\tag\__init__.py", line 62, in pos_tag
tagger = nltk.data.load(_POS_TAGGER)
File "C:\Python25\lib\site-packages\nltk\data.py", line 590, in load
resource_val = pickle.load(_open(resource_url))
File "C:\Python25\lib\site-packages\nltk\data.py", line 669, in _open
return find(path).open()
File "C:\Python25\lib\site-packages\nltk\data.py", line 451, in find
raise LookupError(resource_not_found)
LookupError:
**********************************************************************
Resource 'taggers/maxent_treebank_pos_tagger/english.pickle' not
found. Please use the NLTK Downloader to obtain the resource:
>>> nltk.download().
Searched in:
- 'C:\\nltk_data'
- 'D:\\nltk_data'
- 'E:\\nltk_data'
- 'C:\\Python25\\nltk_data'
- 'C:\\Python25\\lib\\nltk_data'
- 'C:\\Windows\\system32\\config\\systemprofile\\AppData\\Roaming\\nltk_data'
**********************************************************************
I'm trying to run a Python script using exec() from within PHP. My command works fine when I run it directly using a cmd
window, but it produces an error when I run it from exec()
in PHP.
My Python script uses NTLK to find proper nouns. Example command:
"C:\Python25\python.exe" "C:\wamp\projects\python\trunk\tests\find_proper_nouns.py" "I went to London this morning"
returns [London]
when I run it from cmd
, but throws an error in the Apache log when I run the same command from exec()
.The script is defintely getting run OK - if I change the python script to be print "Hello World"
that is returned fine.
I know it's a big ask for anyone to know how to fix this NLTK error, but I could really do with any pointers as to why running it from exec
is different to cmd
. (The command is identical).
I'm running WAMP on Windows 7 with Apache 2.2.11.
Here's the error in the Apache log:
Traceback (most recent call last):
File "C:\wamp\projects\python\trunk\tests\find_proper_nouns_command_line.py", line 6, in <module>
parts = nltk.pos_tag(text)
File "C:\Python25\lib\site-packages\nltk\tag\__init__.py", line 62, in pos_tag
tagger = nltk.data.load(_POS_TAGGER)
File "C:\Python25\lib\site-packages\nltk\data.py", line 590, in load
resource_val = pickle.load(_open(resource_url))
File "C:\Python25\lib\site-packages\nltk\data.py", line 669, in _open
return find(path).open()
File "C:\Python25\lib\site-packages\nltk\data.py", line 451, in find
raise LookupError(resource_not_found)
LookupError:
**********************************************************************
Resource 'taggers/maxent_treebank_pos_tagger/english.pickle' not
found. Please use the NLTK Downloader to obtain the resource:
>>> nltk.download().
Searched in:
- 'C:\\nltk_data'
- 'D:\\nltk_data'
- 'E:\\nltk_data'
- 'C:\\Python25\\nltk_data'
- 'C:\\Python25\\lib\\nltk_data'
- 'C:\\Windows\\system32\\config\\systemprofile\\AppData\\Roaming\\nltk_data'
**********************************************************************
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须运行
nltk.download()
并选择“maxent_treebank_pos_tagger”。您必须制作一个 python 脚本并在其中放入:然后从命令行运行它。它将安装您尚未安装的 POS 标签的数据文件。
执行此操作后,它应该可以工作。
You have to run
nltk.download()
and choose 'maxent_treebank_pos_tagger'. You must make a python script and in it put:then run it from command line. It will install the data files for the POS tagges, which you don't have installed yet.
After you do this it should work.
您的网络服务器可能以除您之外的其他权限运行。可能的问题包括:
提示:在命令提示符和 PHP 进程中执行 set 并检查差异。
Your web server likely runs with other privileges than yourself. Possible problems include:
Tip: execute set in both the command prompt and from the PHP process and check the differences.
从 shell/终端,您可以使用:
它将安装 maxent_treebank_pos_tagger(即 NLTK 中的标准 Treebank POS 标记器)。
From the shell/terminal, you can use:
It will install
maxent_treebank_pos_tagger
(i.e. the standard treebank POS tagger in NLTK).