【问题标题】:Using Pyinstaller with NLTK results in error: can't find nltk_data将 Pyinstaller 与 NLTK 一起使用会导致错误:找不到 nltk_data
【发布时间】:2019-11-29 09:49:16
【问题描述】:

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

当我运行 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

【问题讨论】:

    标签: python nltk exe pyinstaller


    【解决方案1】:

    看来是known bugnltk的PyInstaller的钩子。修复它的一种简单方法是编辑此文件:

    <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 的当前路径。

    【讨论】:

      猜你喜欢
      • 2021-04-26
      • 2011-06-30
      • 2013-11-11
      • 2014-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多