将Pyinstaller与NLTK一起使用会导致错误:找不到nltk_data [英] Using Pyinstaller with NLTK results in error: can't find nltk_data

查看:740
本文介绍了将Pyinstaller与NLTK一起使用会导致错误:找不到nltk_data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试导出一个简单的GUI,该GUI使用NLTK作为exe与Python 3.6和Windows 10配合使用.

I am attempting to export a simple GUI that used NLTK as an exe with Python 3.6 and Windows 10.

当我运行PyInstaller将简单程序冻结为exe时,出现错误: 添加二进制文件和数据文件时找不到"c:\ users \ usr \ nltk_data".

When I run PyInstaller to freeze my simple program as an exe I get the error: Unable to find "c:\users\usr\nltk_data" when adding binary and data files.

当我什至在这里复制nltk_data文件夹时,我在另一个nltk.data.path路径中遇到错误 "c:\ users \ usr \ appdata \ local \ programs \ python \ python36 \ nltk_data"

When I even copied the nltk_data folder here and I get an error in a different nltk.data.path 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

for pyinstaller I run

pyinstaller --onefile -- windowed test_tkinter.py

推荐答案

似乎它是已知错误nltk的PyInstaller挂钩有关.解决此问题的一种简单方法是编辑此文件:

It seems that it is a known bug for the hook of PyInstaller for nltk. An easy way to fix it is to edit this file:

<PythonPath>/Lib/site-packages/PyInstaller/hooks/hook-nltk.py

并对在nltk_data上迭代的行进行注释:

And comment the lines iterate over 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"]

请记住用nltk_data的当前路径替换path_to_nltk_data.

Remember to replace path_to_nltk_data with your currrent path for nltk_data.

这篇关于将Pyinstaller与NLTK一起使用会导致错误:找不到nltk_data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆