gpt4 book ai didi

python-3.x - pyinstaller 和加载 pickle 文件

转载 作者:行者123 更新时间:2023-12-04 20:49:07 25 4
gpt4 key购买 nike

有没有人使用 pyinstaller 从 python 脚本创建 Windows 可执行文件?我正在尝试创建一个可加载 pickle 文件但未成功的可执行文件。

import pickle
filename='test.sav'
try:
model = pickle.load(open(filename, 'rb'))
print('model loaded')
except:
print('An error occurred.')

当使用 python 3 运行时,它可以正常工作并加载 model,但是当使用 pyinstaller 创建的可执行文件运行时,它将通过异常。感谢帮助。

最佳答案

我将 kivy 与 pickle 一起使用,它工作正常。

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.config import Config
import pickle
FRAMES_PER_SECOND = 60.0
Config.set('graphics','multisamples',2)

a = []
class Display(BoxLayout):

def save(self,text):
with open('db.prz','wb') as file:
pickle.dump(text,file)
def loads(self):
with open('db.prz','rb') as file:
a = pickle.load(file)
print(a)

class TestApp(App):
def build(self):
a = Display()
return a

if __name__ == '__main__':
TestApp().run()

kv文件:

<Display>:
BoxLayout:
orientation: 'vertical'
TextInput:
id: kvtext
size_hint_y : 0.2
BoxLayout:
orientation : 'horizontal'
Button:
text: 'Save'
on_press: root.save(kvtext.text)
Button:
text: 'Load'
on_press: root.loads()

规范文件更改:

# -*- mode: python ; coding: utf-8 -*-

from kivy_deps import sdl2, glew

a = Analysis(
datas=[('C:\\Users\\mnt\\Documents\\build_test\\test.kv','.')],
coll = COLLECT(
*[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],

请注意,我没有将我的文件“db.prze”添加到 Analysis()

中的数据中

我还从 github 而不是 pip 安装了 pyi。

如果您正在加载类对象,请从文件中导入它的类。

关于python-3.x - pyinstaller 和加载 pickle 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52861339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com