将Pyinstaller与NLTK一起使用会导致错误:找不到nltk_data
问题内容:
我正在尝试导出一个简单的GUI,该GUI使用NLTK作为exe与Python 3.6和Windows 10一起使用。
当我运行PyInstaller将简单程序冻结为exe时,出现错误:添加二进制文件和数据文件时找不到“ c:\ users \ usr \
nltk_data”。
当我什至在此处复制nltk_data文件夹时,我在另一个nltk.data.path路径“ c:\ users \ usr \ appdata \
local \ programs \ python \ python36 \ nltk_data”中遇到错误
import tkinter as tk
from nltk.corpus import stopwords
sw = stopwords.words('english')
counter = 0
def counter_label(label):
counter = 0
def count():
global counter
counter += 1
label.config(text=sw[counter])
label.after(1000, count)
count()
root = tk.Tk()
root.title("Counting Seconds")
label = tk.Label(root, fg="dark green")
label.pack()
counter_label(label)
button = tk.Button(root, text='Stop', width=25, command=root.destroy)
button.pack()
root.mainloop()
对于pyinstaller我运行
pyinstaller --onefile -- windowed test_tkinter.py
问题答案:
看来这是PyInstaller
for的钩子的已知错误nltk
。解决此问题的一种简单方法是编辑此文件:
<PythonPath>/Lib/site-packages/PyInstaller/hooks/hook-nltk.py
并注释重复的行nltk_data
:
#-----------------------------------------------------------------------------
# Copyright (c) 2005-2018, 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
。