Getting Pyinstaller to recognize the path to Kivy Garden Matplotlib modules

笑着哭i 提交于 2020-07-19 04:41:19

问题


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.


回答1:


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.




回答2:


You should add '~/kivy/garden/matplotlib' to its pathhex in .spec file.

And change hiddenimports=['backend_kivy']



来源:https://stackoverflow.com/questions/41228334/getting-pyinstaller-to-recognize-the-path-to-kivy-garden-matplotlib-modules

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