pyinstaller 和加载泡菜文件 [英] pyinstaller and loading pickle file
问题描述
有没有人使用 pyinstaller
从 python 脚本创建 Windows 可执行文件?我正在尝试创建一个加载 pickle 文件但未成功的可执行文件.
Has anyone worked with pyinstaller
to create windows executables from python scripts? I'm trying to create an executable that loads a pickle file but not successful.
import pickle
filename='test.sav'
try:
model = pickle.load(open(filename, 'rb'))
print('model loaded')
except:
print('An error occurred.')
当使用 python 3 运行时,它可以正常工作并正确加载 model
,但是当使用由 pyinstaller
创建的可执行文件运行时,它将通过异常.感谢帮助.
When run with python 3, it works and loads the model
correctly but when run with the executable created by pyinstaller
, it will through an exception. Help appreciated.
推荐答案
我将 kivy 与 pickle 一起使用,它工作正常.
Im using kivy with pickle and it works correctly.
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()
Note that im not adding my file 'db.prze' to data in Analysis()
我也是从github而不是pip安装了pyi.
I also installed pyi from github not from pip.
如果您正在加载类对象,请从文件中导入其类.
If you are loading class object, import its class from file.
这篇关于pyinstaller 和加载泡菜文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!