不同的nltk导致django和命令行

时间:2015-06-02 23:19:29

标签: python django django-views nltk

我有一个django 1.8视图,如下所示:

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: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的同一目录中运行时它工作正常,它工作正常:

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'做了同样的事情,它也是这样的。

欢迎任何有关发现差异的调试建议。

1 个答案:

答案 0 :(得分:2)

这里的问题是运行django的用户没有权限在/ root读取。

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