将Pyinstaller与NLTK一起使用会导致错误:找不到nltk_data

时间:2019-07-20 21:29:39

标签: python nltk exe pyinstaller

我正在尝试导出一个简单的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”

Route

我运行的pyinstaller

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()

1 个答案:

答案 0 :(得分:1)

似乎它是known bug的PyInstaller钩子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的当前路径。