【发布时间】: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