有时候,我们在打包调用nltk库的应用时,会遇到如下的报错:
Unable to find "nltk_data" when adding binary and data files
解决方法:
修改hook-nltk.py文件,通常此文件位于Anaconda安装文件夹下的site-packages里,比如我的hook-nltk.py位于:D:\anaconda\Lib\site-packages\PyInstaller\hooks\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(("<path_to_nltk_data>", "nltk_data"))
# nltk.chunk.named_entity should be included
hiddenimports = ["nltk.chunk.named_entity"]
把<path_to_nltk_data>修改成自己电脑中