Django和命令行中不同的nltk结果 [英] different nltk results in django and at command line

查看:117
本文介绍了Django和命令行中不同的nltk结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个django 1.8视图,看起来像这样:

I have a django 1.8 view that looks like this:

def sourcedoc_parse(request, sourcedoc_id):
    sourcedoc = Sourcedoc.objects.get(pk=sourcedoc_id)
    nltk.data.path.append('/root/nltk_data')
    new_words = []
    english_vocab = set(w.lower() for w in nltk.corpus.gutenberg.words())    #<---the line where the error occurs
    results = {}

    template = 'sourcedoc_parse.html'
    params = {'sourcedoc': sourcedoc,'results': results, 'new_words': new_words, 'BASE_URL': BASE_URL}

    return render_to_response(template, params, context_instance=RequestContext(request))

它给了我以下错误:

Django Version: 1.8
Python Version: 2.7.6
...
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/rosshartshorn/htdocs/worldmaker/sourcedocs/views.py" in sourcedoc_parse
107.     english_vocab = set(w.lower() for w in nltk.corpus.gutenberg.words())
File "/usr/local/lib/python2.7/dist-packages/nltk/corpus/util.py" in __getattr__
68.         self.__load()

__ load中的文件"/usr/local/lib/python2.7/dist-packages/nltk/corpus/util.py" 56.除了LookupError:提高e

File "/usr/local/lib/python2.7/dist-packages/nltk/corpus/util.py" in __load 56. except LookupError: raise e

Exception Type: LookupError at /sourcedoc/parse/13/
Exception Value: 
**********************************************************************
Resource 'corpora/gutenberg' not found.  Please use the NLTK
Downloader to obtain the resource:  >>> nltk.download()
Searched in:
- '/var/www/nltk_data'
- '/usr/share/nltk_data'
- '/usr/local/share/nltk_data'
- '/usr/lib/nltk_data'
- '/usr/local/lib/nltk_data'
- '/root/nltk_data'
**********************************************************************

尤其奇怪的是,当我在python shell的同一目录中运行时,它运行良好:

What is especially odd is that it works fine when I do it in the same directory in the python shell, it works fine:

Python 2.7.6 (default, Mar 22 2014, 22:59:38) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import nltk
>>> english_vocab = set(w.lower() for w in nltk.corpus.gutenberg.words())
>>> 'jabberwocky' in english_vocab
False
>>> 'monster' in english_vocab
True
>>> nltk.data.path
['/root/nltk_data', '/usr/share/nltk_data', '/usr/local/share/nltk_data', '/usr/lib/nltk_data', '/usr/local/lib/nltk_data']

有人知道在django中在视图中运行它与在python命令行中执行相同操作之间的区别是什么?我使用'python manage.py shell'做过同样的事情,它也可以那样工作.

Does anyone have an idea what is the source of the difference between running it inside a view in django, and doing the same thing at the python command line? I've done the same thing using 'python manage.py shell', and it also works that way.

也欢迎提供有关发现差异的任何调试建议.

Any debugging advice on finding the difference is also welcome.

推荐答案

此处的问题是运行django的用户无权在/root位置读取.

The problem here is that the user running django don't have permission to read at /root.

在运行django shell时不会发生这种情况,因为您是以root身份运行shell的,但是服务器是以www用户身份运行的(请参阅第一个目录,其中nltk搜索为/var/www/nltk_data,即该目录的主目录) www用户).

It does not happens when running django shell because you are running the shell as root, but the server is running as the www user (see, the first directory where nltk search is /var/www/nltk_data, the home dir for the www user).

这篇关于Django和命令行中不同的nltk结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆