从命令行运行脚本与使用 PHP 的 exec() 运行脚本有什么区别?

发布于 2024-08-22 05:18:51 字数 1960 浏览 4 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤寂小茶 2024-08-29 05:18:52

您必须运行nltk.download()并选择“maxent_treebank_pos_tagger”。您必须制作一个 python 脚本并在其中放入:

#!/usr/bin/python
import nltk
nltk.download('maxent_treebank_pos_tagger');

然后从命令行运行它。它将安装您尚未安装的 POS 标签的数据文件。

执行此操作后,它应该可以工作。

You have to run nltk.download() and choose 'maxent_treebank_pos_tagger'. You must make a python script and in it put:

#!/usr/bin/python
import nltk
nltk.download('maxent_treebank_pos_tagger');

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.

听不够的曲调 2024-08-29 05:18:52

您的网络服务器可能以除您之外的其他权限运行。可能的问题包括:

  • 路径/文件权限:Web 服务器用户能否访问其所需的文件?
  • 不同的环境:是否设置了所有必要的环境变量(PATH、Python 特定的东西……)?
  • 配置:Python 或模块是否有针对每个用户的配置?

提示:在命令提示符和 PHP 进程中执行 set 并检查差异。

Your web server likely runs with other privileges than yourself. Possible problems include:

  • Path/file permission: can the web server user access the files it needs?
  • Different environment: are all necessary environment variables (PATH, Python-specific stuff, …) set?
  • Configuration: are there per-user configurations for Python or the module?

Tip: execute set in both the command prompt and from the PHP process and check the differences.

乖乖 2024-08-29 05:18:52

从 shell/终端,您可以使用:

sudo python -m nltk.downloader maxent_treebank_pos_tagger

它将安装 maxent_treebank_pos_tagger(即 NLTK 中的标准 Treebank POS 标记器)。

From the shell/terminal, you can use:

sudo python -m nltk.downloader maxent_treebank_pos_tagger

It will install maxent_treebank_pos_tagger (i.e. the standard treebank POS tagger in NLTK).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文