问题描述
这类似于: PyInstaller中的基维花园-卡住了尝试跟踪导入,只是我将提供更多详细信息,希望这可以使某人更容易进行测试并提供具体的解决方案....
我已经使用Kivy 1.9.1构建了Python 2.7.13应用程序,并尝试使用Pyinstaller将其打包到Windows 10中的Windows.简而言之-我无法让Pyinstaller拾取Garden模块,尤其是我用来显示数据的matplotlib模块.该花园模块是通过以下方式安装的:
garden.bat install matplotlib
我的应用程序太大且太复杂,因此无法在此处发布,因此我在网上找到了一个示例,其中包含尝试以与我执行该操作基本相同的方式导入和使用该模块的示例.这是Python文件,从Python解释器运行时效果很好:
import matplotlib
matplotlib.use('module://kivy.garden.matplotlib.backend_kivy')
from matplotlib.figure import Figure
from numpy import arange, sin, pi
from kivy.app import App
import numpy as np
from matplotlib.mlab import griddata
from kivy.garden.matplotlib.backend_kivy import FigureCanvas,\
NavigationToolbar2Kivy
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from matplotlib.transforms import Bbox
from kivy.uix.button import Button
from kivy.graphics import Color, Line, Rectangle
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
fig, ax = plt.subplots()
X = np.arange(-508, 510, 203.2)
Y = np.arange(-508, 510, 203.2)
X, Y = np.meshgrid(X, Y)
Z = np.random.rand(6, 6)
plt.contourf(X, Y, Z, 100, zdir='z', offset=1.0, cmap=cm.hot)
plt.colorbar()
ax.set_ylabel('Y [mm]')
ax.set_title('NAILS surface')
ax.set_xlabel('X [mm]')
canvas = fig.canvas
def callback(instance):
global fig, ax
X = np.arange(-508, 510, 203.2)
Y = np.arange(-508, 510, 203.2)
X, Y = np.meshgrid(X, Y)
Z = 1000*np.random.rand(6, 6)
plt.clf()
plt.contourf(X, Y, Z, 100, zdir='z', offset=1.0, cmap=cm.hot)
plt.colorbar()
canvas.draw()
class MatplotlibTest(App):
title = 'Matplotlib Test'
def build(self):
fl = BoxLayout(orientation="vertical")
a = Button(text="press me", height=40, size_hint_y=None)
a.bind(on_press=callback)
fl.add_widget(canvas)
fl.add_widget(a)
return fl
if __name__ == '__main__':
MatplotlibTest().run()
Pyinstaller以及Python解释器都放在我的Windows路径上,因此当我通过Windows Powershell之类将上述文件(名为mplTest.py)传递给Python解释器时,
python mplTest.py
一切都像魅力.但是,尝试使用以下命令将其打包:
pyinstaller mplTest.py
产生预期的./dist/和./build/目录,以及一个pyinstaller规范文件.如果我导航到./dist/mplTest/目录并尝试运行mplTest.exe文件(由pyinstaller生成的可执行文件),则将得到以下内容作为日志消息的一部分:
[WARNING ] stderr: Traceback (most recent call last):
[WARNING ] stderr: File "mplTest.py", line 10, in <module>
[WARNING ] stderr: ImportError: No module named garden.matplotlib.backend_kivy
我尝试使用以下方法修改.spec文件中的hidden_imports:
hiddenimports=['garden.matplotlib.backend_kivy'],
但是当尝试使用pyinstaller编译.spec文件时,我最终得到了这一行:
362 ERROR: Hidden import 'garden.matplotlib.backend_kivy' not found
有人可以在这里提出建议吗?提到的链接SO线程中的一位用户使用:
garden.bat install --app matplotlib
在安装这些文件的目录中创建一个./libs/garden/目录,所以也许有人可以向我展示如何从spec文件中链接到这些文件?
我真的很反对这个想法,投入这么多的开发时间被困在一件可能微不足道的事情上是很痛苦的…….如果有必要,我很乐意提供更多信息. /p>
谢谢.
我已通过重新安装 garden.matplotlib
来解决此问题.您需要使用:
garden install matplotlib --kivy
如果安装没有-kivy 标签的 garden.matplotlib ,它将安装在〜/.kivy/garden/matplotlib 中文件夹. Pyinstaller在那儿找不到它.设置-kivy 标记后,该标记将被全局安装. Pyinstaller可以在其中捕获并自动包含它.
This is similar to: Kivy Garden in PyInstaller - stuck trying to trace import except that I'll provide much more detail to hopefully make this easier for someone to test out and provide a concrete solution....
I've built a Python 2.7.13 application using Kivy 1.9.1 and am attempting to package it for Windows, within Windows 10, with Pyinstaller. In short - I cannot get Pyinstaller to pick up the Garden modules - specifically the matplotlib module, which I am using to display data. This garden module was installed via:
garden.bat install matplotlib
My application is far too large and complicated to post here, so I've found an example online to include which tries to import and use the module in essentially the same way that I'm doing it. Here is the Python file, which works fine when ran from the Python interpreter:
import matplotlib
matplotlib.use('module://kivy.garden.matplotlib.backend_kivy')
from matplotlib.figure import Figure
from numpy import arange, sin, pi
from kivy.app import App
import numpy as np
from matplotlib.mlab import griddata
from kivy.garden.matplotlib.backend_kivy import FigureCanvas,\
NavigationToolbar2Kivy
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from matplotlib.transforms import Bbox
from kivy.uix.button import Button
from kivy.graphics import Color, Line, Rectangle
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
fig, ax = plt.subplots()
X = np.arange(-508, 510, 203.2)
Y = np.arange(-508, 510, 203.2)
X, Y = np.meshgrid(X, Y)
Z = np.random.rand(6, 6)
plt.contourf(X, Y, Z, 100, zdir='z', offset=1.0, cmap=cm.hot)
plt.colorbar()
ax.set_ylabel('Y [mm]')
ax.set_title('NAILS surface')
ax.set_xlabel('X [mm]')
canvas = fig.canvas
def callback(instance):
global fig, ax
X = np.arange(-508, 510, 203.2)
Y = np.arange(-508, 510, 203.2)
X, Y = np.meshgrid(X, Y)
Z = 1000*np.random.rand(6, 6)
plt.clf()
plt.contourf(X, Y, Z, 100, zdir='z', offset=1.0, cmap=cm.hot)
plt.colorbar()
canvas.draw()
class MatplotlibTest(App):
title = 'Matplotlib Test'
def build(self):
fl = BoxLayout(orientation="vertical")
a = Button(text="press me", height=40, size_hint_y=None)
a.bind(on_press=callback)
fl.add_widget(canvas)
fl.add_widget(a)
return fl
if __name__ == '__main__':
MatplotlibTest().run()
Pyinstaller, as well as the Python interpreter, are put on my windows path, so when I pass the above file (named mplTest.py) to the Python interpreter via the Windows Powershell like
python mplTest.py
everything works like a charm. However, the attempt to package this with the command:
pyinstaller mplTest.py
yields the ./dist/ and ./build/ directories as expected, along with a pyinstaller spec file. If I navigate to the ./dist/mplTest/ directory and try to run the file mplTest.exe (the executable generated by pyinstaller), I get the following as a part of the log message:
[WARNING ] stderr: Traceback (most recent call last):
[WARNING ] stderr: File "mplTest.py", line 10, in <module>
[WARNING ] stderr: ImportError: No module named garden.matplotlib.backend_kivy
I've tried to modify the hidden_imports inside of the .spec file with:
hiddenimports=['garden.matplotlib.backend_kivy'],
but when trying to compile the .spec file with pyinstaller, I eventually get the line:
362 ERROR: Hidden import 'garden.matplotlib.backend_kivy' not found
Can someone please make a suggestion here? One user in the linked SO thread mentioned using:
garden.bat install --app matplotlib
to create a ./libs/garden/ directory where these are installed, so maybe someone can show me how to link to these from within the spec file?
I'm really beating my head against this one and it's painful to have put in so many development hours to be stuck on something that is probably so trivial....I'm happy to supply more information if necessary.
Thanks in advance.
I've fixed the problem by reinstalling garden.matplotlib
You need to use:
garden install matplotlib --kivy
When you install garden.matplotlib without the --kivy tag, it gets installed in the ~/.kivy/garden/matplotlib folder. Pyinstaller can't find it there. When you set the --kivy tag, it gets installed globally. Pyinstaller can catch it there and will include it automatically.
这篇关于使Pyinstaller识别Kivy Garden Matplotlib模块的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!