我正在尝试打包一个需要多次导入的应用程序,其中包括 matplotlib.pyplot
kivy 应用程序(经过简化,但仍然有效)是:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
import matplotlib.pyplot
Builder.load_string("""
<MyWidget>:
id: my_widget
FileChooserIconView:
id: filechooser
on_selection: my_widget.selected(filechooser.selection)
Image:
id: image
source: ""
""")
class MyWidget(BoxLayout):
def selected(self,filename):
self.ids.image.source = filename[0]
class MyApp(App):
def build(self):
return MyWidget()
if __name__ == '__main__':
MyApp().run()
这个应用程序使用spyder在Python中完美运行。
但是,当我尝试将其打包为独立的 kivy 应用程序时,它给我错误最大递归深度超出。
我很惊讶,我不知道问题是什么,因为:
1.应用程序中没有递归函数。
2.在开发和测试过程中,在pythonspyder中完美运行,唯一的问题是在打包过程中。
3.我尝试了多种选项,包括注释掉几个部分,最令人惊讶的是,当我注释掉 import matplotlib.pyplot 时,应用程序打包得很好。然而,我需要 matplotlib.pyplot 这个应用程序,所以把它拿出来不是一个选择。
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
#import matplotlib.pyplot
Builder.load_string("""
<MyWidget>:
id: my_widget
FileChooserIconView:
id: filechooser
on_selection: my_widget.selected(filechooser.selection)
Image:
id: image
source: ""
""")
class MyWidget(BoxLayout):
def selected(self,filename):
self.ids.image.source = filename[0]
class MyApp(App):
def build(self):
return MyWidget()
if __name__ == '__main__':
MyApp().run()
上面的代码可以很好地工作并且封装良好。
导入 kivy 应用程序的文件大小是否有限制?我已经尝试使用 sys.setrecursionlimit(high number) 增加递归限制,但这不是解决此问题的方法。 我真的迷失了。任何见解表示赞赏。
谢谢
编辑 2/4/2019: 有人提出问题:pyinstaller creating EXE RuntimeError: maximum recursion depth exceeded while calling a Python object是重复的并回答了这个问题。虽然这绝对是一个相关的问题并且很有帮助,但我的错误发生在创建 kivy 包的第一阶段: python -m PyInstaller --name touchtracer Examples-path\demo\touchtracer\main.py
最佳答案
非常感谢所有试图提供帮助的人。 我找到了答案,希望它能帮助其他尝试创建 kivy 包但导入 python 模块时出现问题的人。
一旦您准备好 main.py
脚本即可打包:
1.按照说明开始
https://kivy.org/doc/stable/guide/packaging-windows.html
并执行第一步:
python -m PyInstaller --name touchtracer examples-path\demo\touchtracer\main.py
这将为您提供超出最大递归深度
的错误或最初给您带来的任何错误。不用担心。此步骤的目的是创建一个初始 spec
文件。
2.打开 spec
文件并添加 kivy
说明为您提供的所有额外内容
https://kivy.org/doc/stable/guide/packaging-windows.html
即:
from kivy.deps import sdl2, glew
Tree('examples-path\\demo\\touchtracer\\'),
*[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
3. 除此之外,在规范文件的开头添加以下内容:
import sys
sys.setrecursionlimit(5000) # (or some big number)
4.还可以在隐藏导入中添加您可能需要的任何导入。
hiddenimports=[] # change to (example importing pandas and matplotlib) hiddenimports=['pandas', 'matplotlib']
5.只需按照最后一步操作
https://kivy.org/doc/stable/guide/packaging-windows.html
即:
python -m PyInstaller touchtracer.spec
并构建您的应用
关于python - kivy 中超出了最大递归深度,但仅限于打包时,而不是在 python 中开发应用程序时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54471813/