有时候,我们在打包调用nltk库的应用时,会遇到如下的报错:
Unable to find "nltk_data" when adding binary and data files
解决方法:
修改hook-nltk.py文件,通常此文件位于Anaconda安装文件夹下的site-packages里,比如我的hook-nltk.py位于:D:anacondaLibsite-packagesPyInstallerhookshook-nltk.py
修改此文件,具体如下:
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(("", "nltk_data"))
# nltk.chunk.named_entity should be included
hiddenimports = ["nltk.chunk.named_entity"]
把
datas.append(("D:\nltk_data", "nltk_data"))
注意:用双斜杠表示文件夹的路径
修改后的hook-nltk.py文件内容如下:
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(("D:\nltk_data", "nltk_data"))
# nltk.chunk.named_entity should be included
hiddenimports = ["nltk.chunk.named_entity"]
如果找不到nltk_data的文件夹,可采用如下方法:
from nltk import data
print(data.find('.'))



