【问题标题】:FileNotFoundError: [Errno 2] No such file or directory, Pyinstaller searching for a directory \\_MEI97962\\cmudict\\VERSIONFileNotFoundError: [Errno 2] 没有这样的文件或目录,Pyinstaller 正在搜索目录 \\_MEI97962\\cmudict\\VERSION
【发布时间】:2020-09-01 11:05:39
【问题描述】:

我正在尝试使用 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()

【问题讨论】:

  • with open(path, 'rb') as fp: 这是你代码的一部分吗? path 是有效位置吗?浏览并检查C:\\Users\\User\\AppData\\Local\\Temp\\_MEI97962\\cmudict\\VERSION 是否也存在
  • @CoolCloud 我刚刚添加了我认为导致问题的部分代码。这条路不存在。我不知道它包含什么。
  • 您发布的代码不应该是错误的原因,因为没有代码涉及打开文件。它只是使用来自askopenfilename() 的选定文件更新标签。
  • @acw1668我刚刚添加了整个代码

标签: python tkinter pyinstaller


【解决方案1】:

首先,永远不要在--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/

【讨论】:

  • 感谢您的回答。我按照说明操作,但仍然出现错误: pyinstaller: error: argument --add-data: invalid add_data_or_binary value: 'C:/Users/User/AppData/Local/Temp/_MEI97962:cmudict/' 这就是我尝试的方式在命令行上调用:PyInstaller --add-data=C:/Users/User/AppData/Local/Temp/_MEI97962:cmudict/ main.spec
  • 不,您不想添加临时目录,因为在您运行它之前它不存在。 source 应该指向原来的。运行import cmudict; print(cmudict.__path__) 以找到您安装的cmudict
  • 很抱歉问了很多次,但我仍然遇到错误。我跑了import cmudict; print(cmudict.__path__) 并得到['C:\\Users\\User\\Documents\\Python\\Rhymer\\venv\\lib\\site-packages\\cmudict'] 作为路径。我错误地使用了--add-data 参数。使用可用信息,在命令行中进行完整 PyInstaller 调用的正确方法是什么。我正在使用 Python 3.8.1。
  • PyInstaller --add-data=C:\Users\User\Documents\Python\Rhymer\venv\lib\site-packages\cmudict\VERSION:cmudict main.py
  • 糟糕,您使用的是 Windows。使用; 而不是:PyInstaller --add-data=C:\Users\User\Documents\Python\Rhymer\venv\lib\site-packages\cmudict\VERSION;cmudict main.py
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-19
  • 2022-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-03
相关资源
最近更新 更多