FileNotFoundError: [Errno 2] No such file or directory, Pyinstaller searching for a directory \\_MEI97962\\cmudict\\VERSION

FileNotFoundError: [Errno 2] No such file or directory, Pyinstaller searching for a directory \\_MEI97962\\cmudict\\VERSION

我正在尝试使用 pyinstaller 将我的代码打包为一个 exe。我运行这段代码来打包它:

pyinstaller --onefile main.py

我成功地将我的代码作为一个 exe 文件获取,但是当我 运行 它时,我收到以下错误:

Traceback (most recent call last):
  File "main.py", line 3, in <module>
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "c:\users\user\documents\python\rhymer\venv\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 493, in exec_module
    exec(bytecode, module.__dict__)
  File "pronouncing\__init__.py", line 5, in <module>
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "c:\users\user\documents\python\rhymer\venv\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 493, in exec_module
    exec(bytecode, module.__dict__)
  File "cmudict\__init__.py", line 13, in <module>
  File "pkg_resources\__init__.py", line 1156, in resource_string
  File "pkg_resources\__init__.py", line 1401, in get_resource_string
  File "pkg_resources\__init__.py", line 1540, in _get
  File "c:\users\user\documents\python\rhymer\venv\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 341, in get_data
    with open(path, 'rb') as fp:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\User\AppData\Local\Temp\_MEI97962\cmudict\VERSION'
[13072] Failed to execute script main

exe 正在尝试搜索名为 cmudict\VERSION 的文件夹,但找不到。

有什么建议吗?

编辑* 这是我的代码:

import tkinter as tk
from tkinter.filedialog import askopenfilename
import pronouncing

def open_file(fileLabel):
    """Open a file for editing."""
    global filepath
    filepath = askopenfilename(
        filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")]
    )
    if not filepath:
        return
    fileName = filepath.split('/')[-1][:-4]
    fileLabel.config(text=fileName + ' loaded')

def findRhyme(x):
    file = open(x, "r", encoding='UTF-8')
    word = wordEnrty.get()
    txt_edit.delete('1.0', tk.END)
    text = file.read().splitlines()
    text = [i for i in text if not i == '']
    text = [i for i in text if not i[0] == 'P' and not i[-1] == '"']
    Rhyme = [i for i in text if i.split()[-1] in pronouncing.rhymes(word)]
    Rhyme ='\n'.join(Rhyme)
    txt_edit.insert(tk.END, Rhyme)

window = tk.Tk()
window.title("Rhymer")
window.rowconfigure(0, weight=1)
window.columnconfigure(1, weight=1)

txt_edit = tk.Text(window)
fr_buttons = tk.Frame(window, relief=tk.RAISED, bd=2)
fileLabel = tk.Label(fr_buttons, text="No file loaded")
wordEnrty = tk.Entry(fr_buttons)
btn_open = tk.Button(fr_buttons, text="Load Master File", command=lambda:open_file(fileLabel))
btn_rhyme = tk.Button(fr_buttons, text="Find Rhyme", command=lambda:findRhyme(filepath))

btn_open.grid(row=0, column=0, padx=5, pady=5)
wordEnrty.grid(row=0, column=1, padx=5)
btn_rhyme.grid(row=0, column=2, padx=5)
fileLabel.grid(row=0, column=3, sticky='e')
fr_buttons.grid(row=2, column=0, sticky='e'+'w')
txt_edit.grid(row=1, column=0)

window.mainloop()

首先,永远不要在 --onefile 模式下调试 FileNotFoundErrors。 --onefile 本质上是一个包含 --onedir 构建的 zip,它在运行时将其解压缩到一个临时目录中。因为它们都是临时的,所以如果文件存在,通常是不可能的,因为它们会在您的应用程序关闭后立即被删除。

您遇到的问题是 cmudict 包含一个名为 VERSION 的文件,PyInstaller 未收集该文件并且您的应用中缺少该文件。

如果您使用的是 .spec 文件(只是一个 Python 脚本),这很容易修复。添加:

from PyInstaller.utils.hooks import collect_data_files

到规范的顶部,然后将 a = Analysis(...) 中的 datas=[] 扩展为:

datas=collect_data_files('cmudict')

然后构建使用:

PyInstaller main.spec

如果您更愿意使用命令行,那么您需要使用--add-data=source:dest 告知PyInstaller。 source 是 Python 安装中 cmudict/VERSION 文件的完整文件路径。 dest 只是 cmudict/.