pyinstaller and loading pickle file

落爺英雄遲暮 提交于 2021-02-19 03:56:07

问题


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.')

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.


回答1:


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 file:

<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()

Spec File Changes:

# -*- 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)],

Note that im not adding my file 'db.prze' to data in Analysis()

I also installed pyi from github not from pip.

If you are loading class object, import its class from file.



来源:https://stackoverflow.com/questions/52861339/pyinstaller-and-loading-pickle-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!