PyInstaller的hook文件夹在处理nltk时会出现这样的错误,开发者的问题,跟我们没关系。我的电脑上使用的是pyinstaller-3.5。
现提供一种简单的解决办法:
1. 找到hook-nltk.py文件,通常此文件的目录为:
<PythonPath>/Lib/site-packages/PyInstaller/hooks/hook-nltk.py
在我的电脑上就是:
C:\Users\user\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\PyInstaller\hooks\hook-nltk.py
2.修改此.py文件:
#-----------------------------------------------------------------------------
# Copyright (c) 2005-2019, PyInstaller Development Team.
#
# Distributed under the terms of the GNU General Public License with exception
# for distributing bootloader.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
# hook for nltk
import nltk
from PyInstaller.utils.hooks import collect_data_files
# add datas for nltk
datas = collect_data_files('nltk', False)
# loop through the data directories and add them
# for p in nltk.data.path:
# datas.append((p, "nltk_data"))
datas.append(("<path_to_nltk_data>", "nltk_data"))
# nltk.chunk.named_entity should be included
hiddenimports = ["nltk.chunk.named_entity"]
记住一定要把<path_to_nltk_data>替换成自己电脑上nltk_data文件夹所在的路径!
3. 修改后的.py文件:
在我的电脑上nltk_data文件夹的路径为:c:\users\user\appdata\roaming\nltk_data,所以修改后的hook-nltk.py为:
# hook for nltk
import nltk
from PyInstaller.utils.hooks import collect_data_files
# add datas for nltk
datas = collect_data_files('nltk', False)
# loop through the data directories and add them
#for p in nltk.data.path:
# datas.append((p, "nltk_data"))
datas.append(("c:\\users\\user\\appdata\\roaming\\nltk_data", "nltk_data"))
# nltk.chunk.named_entity should be included
hiddenimports = ["nltk.chunk.named_entity"]
注意:用双斜杠表示文件夹路径!
若一时半会手动找不到nltk_data文件夹的路径,可使用两行python代码找到:
from nltk import data
print(data.find('.'))