FileNotFoundError:[错误2]没有此类文件或目录,Pyinstaller搜索目录\\ _ 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,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,sticky='e'+'w')
txt_edit.grid(row=1,column=0)

window.mainloop()

解决方法

首先,切勿在{{1​​}}模式下调试FileNotFoundError--onefile本质上是一个包含--onefile构建的zip,可在运行时将其解压缩到一个临时目录中。由于这都是临时性的,因此通常无法确定文件是否存在,因为一旦您的应用关闭,它们就会被删除。

您遇到的问题是--onedir包含一个名为cmudict的文件,PyInstaller无法收集该文件,并且您的应用程序中缺少该文件。

如果您使用的是VERSION文件(只是Python脚本),则很容易修复。添加:

.spec

在规范顶部,然后将from PyInstaller.utils.hooks import collect_data_files 中的datas=[]扩展为:

a = Analysis(...)

然后使用以下内容进行构建:

datas=collect_data_files('cmudict')

如果您想使用命令行,则需要使用PyInstaller main.spec 告知PyInstaller。 --add-data=source:dest是Python安装中source文件的完整文件路径。 cmudict/VERSION就是dest