python - exe文件执行失败
问题描述
我正在使用 Python 和 tkinter 构建一个简单的预测 GUI。它在 Jupyter 笔记本中运行良好,并且当我使用 .py 将文件转换为 .py 时nbconvert
。由于我想将该工具传递给没有 python 的人,因此我已使用pyinstaller --onefile Prediction.py
.
exe 的构建没有错误,但是当我在 cmd 中运行 exe 时,我得到了FileNotFoundError: [Errno 2] File b'SRV_Platforms_Yield.csv' does not exist: b'SRV_Platforms_Yield.csv'[5404] Failed to execute script Prediction
这意味着它找不到我的训练文件。我怎样才能让它工作?
这是我的预测工具的代码:
import pandas as pd
import numpy as np
df = pd.read_csv('SRV_Platforms_Yield.csv')
df.rename(columns={'REPORT FAMILY':'REPORT_FAMILY', 'FPY+':'FPY'},inplace=True)
df['FPY'] = df['FPY'].apply(lambda x: np.nan if x in ['-'] else x[:-1]).astype(float)/100
df = df[['REPORT_FAMILY', 'FPY_TESTED', 'FPY']]
df['REPORT_FAMILY'] = df['REPORT_FAMILY'].astype(str)
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
df["REPORT_FAMILY"] = le.fit_transform(df["REPORT_FAMILY"])
print("Class mapping: ")
for i, item in enumerate(le.classes_):
print(item, "-->", i)
x = df.iloc[:,:-1]
y = df.iloc[:,-1]
from sklearn.ensemble import RandomForestRegressor
rf = RandomForestRegressor(n_estimators= 1000, random_state=42)
rf.fit(x, y)
#tkinter GUI
import tkinter as tk
#creating the window
window = tk.Tk()
window.title("FPY+ Prediction")
canvas1 = tk.Canvas(window, width = 550, height = 250)
canvas1.pack()
t = tk.Text(window)
canvas1.create_window(270, 250)
for i, item in enumerate(le.classes_):
t.insert(tk.INSERT, f"{item}-->{i}\n")
t.pack()
# New_Interest_Rate label and input box
label1 = tk.Label(window, text='Type REPORT FAMILY: ')
canvas1.create_window(50, 100, window=label1)
entry1 = tk.Entry (window) # create 1st entry box
canvas1.create_window(270, 100, window=entry1)
# New_Unemployment_Rate label and input box
label2 = tk.Label(window, text='Type FPY_TESTED: ')
canvas1.create_window(60, 130, window=label2)
entry2 = tk.Entry (window) # create 2nd entry box
canvas1.create_window(270, 120, window=entry2)
def values():
global New_REPORT_FAMILY #our 1st input variable
New_REPORT_FAMILY = entry1.get()
global New_FPY_TESTED #our 2nd input variable
New_FPY_TESTED = float(entry2.get())
Prediction_result = ('Predicted FPY: ', rf.predict([[New_REPORT_FAMILY ,New_FPY_TESTED]]))
label_Prediction = tk.Label(window, text= Prediction_result, bg='yellow')
canvas1.create_window(260, 180, window=label_Prediction)
button1 = tk.Button (window, text='Predict FPY',command=values, bg='grey') # button to call the 'values' command above
canvas1.create_window(270, 150, window=button1)
window.mainloop()
解决方案
您应该将文件“SRV_Platforms_Yield.csv”和 .exe 文件放在同一个文件夹中
推荐阅读
- python - 在python中反转dict并访问其值的正确方法是什么?
- jupyter-notebook - 如何通过多跳的 xshell 连接到 jupyter 笔记本?
- c++ - 将节点划分为线程c ++
- javascript - 打印通过 ajax 调用获取的文档
- java - 如何导入 org.apache.axis.utils.JavaUtils
- javascript - HTML - 如何连接两个下拉菜单并使第二个可点击
- python - 无法编码为 UTF-8 - json 消息 - Python
- javascript - 将 PHP 类对象传递给 Javascript 并在 Javascript 函数中访问其属性
- php - XMLHttpRequest+Magento , 第二个请求重定向到主页
- macos - 如何解码 FPLY(Airplay Mirroring) DRM key