我有一个基本的Django项目。我正在研究的一个特性是计算.txt文件中最常见的单词数,例如一本大型公共域书籍。我已经使用过滤掉了“秒词”(在SEO语言中,意思是多余的单词,比如‘the’、‘you’等等)。
无论如何,当Django提供模板时,我会得到这个调试跟踪:
资源[9][0m未找到]。请使用NLTK下载程序获取资源:[3100万<<<导入nltk nltk.download(‘秒词’) [0m ]以获得更多信息(参见:https://www.nltk.org/data.html )
所以我需要下载秒针语库。为了解决这个问题,我只需在远程服务器上打开Python并调用以下两行简单的代码:
<<< import nltk
<<< nltk.download('stopwords')在其他地方都有详细报道。这解决了这个问题,但只是暂时的。一旦REPL会话在我的远程服务器上终止,错误就会返回,因为秒词文件就会蒸发。
当我使用git将更改推送到Heroku上的远程服务器时,我注意到了一些奇怪的事情。检查一下这个:
remote: -----> Python app detected
remote: -----> No change in requirements detected, installing from cache
remote: -----> Installing pip 20.1.1, setuptools 47.1.1 and wheel 0.34.2
remote: -----> Installing SQLite3
remote: -----> Installing requirements with pip
remote: -----> Downloading NLTK corpora…
remote: ! 'nltk.txt' not found, not downloading any corpora
remote: ! Learn more: https://devcenter.heroku.com/articles/python-nltk
remote: -----> $ python manage.py collectstatic --noinput
remote: 122 static files copied to '/tmp/build_f2f9d10f/staticfiles', 388 post-processed.发展中心链路有点像存根,这意味着它不是很详细。充其量是稀疏的。本文说,要使用Python,需要向项目目录中添加一个nltk.txt文件,该目录指定要下载的对象列表。因此,我继续创建了一个nltk文本文件,其中包含:
语料库
这是活动的nltk.txt当前位于我的项目目录中。除了coprora之外,我还尝试在nltk.txt中添加以下三个条目的各种组合:
语料库英语
我试着把所有四个加起来,只加两个,加一个。例如,下面是我逐字尝试的另一个nltk.txt。我的感觉是,我真正需要的主要内容就是corpora,所以这是我现在正在使用的nltk.txt中唯一的条目。在使用corpora时,当我推送更改和Heroku构建环境时,我会看到这个错误和跟踪:
remote: -----> Downloading NLTK corpora…
remote: -----> Downloading NLTK packages: corpora english stopwords corpus
remote: /app/.heroku/python/lib/python3.6/runpy.py:125: RuntimeWarning: 'nltk.downloader' found in sys.modules after import of package 'nltk', but prior to execution of 'nltk.downloader'; this may result in unpredictable behaviour
remote: warn(RuntimeWarning(msg))
remote: [nltk_data] Error loading corpora: Package 'corpora' not found in
remote: [nltk_data] index
remote: Error installing package. Retry? [n/y/e]
remote: Traceback (most recent call last):
remote: File "/app/.heroku/python/lib/python3.6/runpy.py", line 193, in _run_module_as_main
remote: "__main__", mod_spec)
remote: File "/app/.heroku/python/lib/python3.6/runpy.py", line 85, in _run_code
remote: exec(code, run_globals)
remote: File "/app/.heroku/python/lib/python3.6/site-packages/nltk/downloader.py", line 2538, in <module>
remote: halt_on_error=options.halt_on_error,
remote: File "/app/.heroku/python/lib/python3.6/site-packages/nltk/downloader.py", line 790, in download我显然没有正确地使用nltk.txt,因为它找不到corpora包。我可以安装nltk并在本地dev服务器上运行它,但我剩下的问题是:,如何使Heroku在这种情况下远程处理nltk,
用户麦可·戈德为多个堆栈溢出问题提供了相同的答案,您可以在项目根目录中创建一个bin目录,并添加一个post_compile bash脚本和一个install_nltk_data脚本。但是,这不再是必要的,因为上游保持器Kenneth Reitz实现了一个简单的解决方案。现在所需要的就是添加一个包含您需要的库的nltk.txt。但我这样做了,我仍然得到上面的错误。
官方网站一般如何使用图书馆的文档和如何安装对Heroku没有帮助,因为Heroku似乎处理nltk的方式不同。
发布于 2020-11-29 14:24:05
尤里卡!我让它起作用了。我的问题是下载nltk库的名称。当实际名称为stoplist时,我尝试了stopwords。哈!现在,我的nltk.txt的内容很简单:stopwords。当我推到Heroku时,构建成功了,我的网站现在已经在网上部署和访问。
特别感谢@黑暗骑士在他的回答的评论部分的耐心和洞察力。
https://stackoverflow.com/questions/64945076
复制相似问题