pyinstaller:使用matplotlib构建python exe

我一直试图复制这个post的例子,用pyinstaller生成一个可执行文件但没有成功.我在Linux操作系统上.

我尝试构建的exe是基于此代码(之后是test.py)

from pylab import *
from matplotlib import pyplot as plt

figure(1, figsize=(6,6))
ax = axes([0.1, 0.1, 0.8, 0.8])

labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
fracs = [15, 30, 45, 10]
explode=(0, 0.05, 0, 0)

pie(fracs, explode=explode, labels=labels,
                autopct='%1.1f%%', startangle=90)

title('Pie Chart Example', bbox={'facecolor':'0.8', 'pad':5})

show()

要创建我运行的可执行文件

pyinstaller --onefile test.py

我尝试使用3.0版(pip安装中的官方版本)和pyinstaller的3.1.dev版本.两者都编译但是当我运行可执行文件时我得到了

./test 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyimod03_importers.py", line 363, in load_module
    exec(bytecode, module.__dict__)
  File "/usr/share/pyshared/pylab.py", line 1, in <module>
    from matplotlib.pylab import *
  File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyimod03_importers.py", line 363, in load_module
    exec(bytecode, module.__dict__)
  File "/usr/share/pyshared/matplotlib/__init__.py", line 157, in <module>
    from matplotlib.compat import subprocess
ImportError: No module named compat
test returned -1

我读到可能的错误可能与我的$PYTHONPATH有关(见post)
但即使有一个干净的$PYTHONPATH environement,我也有同样的错误.

另外我不明白为什么可执行文件在/ usr /目录中查找文件时它应该是独立的.

任何建议都会受到欢迎.
谢谢.

PS:这是使用pyinstaller v3.0进行编译的输出.注意
为了使它工作,我必须在文件中评论’PyQt4’和’PyQt5′
/usr/local/lib/python2.7/dist-packages/PyInstaller/hooks/hook-PIL.py,如post所述.

pyinstaller --onefile test.py 
22 INFO: PyInstaller: 3.0
22 INFO: Python: 2.7.6
22 INFO: Platform: Linux-3.16.0-30-generic-x86_64-with-Ubuntu-14.04-trusty
40 INFO: wrote /home/paugam/Src/stackimage/test.spec
44 INFO: UPX is not available.
45 INFO: Extending PYTHONPATH with /home/paugam/Src/stackimage
45 INFO: checking Analysis
45 INFO: Building Analysis because out00-Analysis.toc is non existent
45 INFO: Initializing module dependency graph...
46 INFO: Initializing module graph hooks...
85 INFO: running Analysis out00-Analysis.toc
102 INFO: Analyzing /home/paugam/Src/stackimage/test.py
135 INFO: Processing pre-find module path hook   distutils
6044 INFO: Processing pre-find module path hook   site
6044 INFO: site: retargeting to fake-dir '/usr/local/lib/python2.7/dist-packages/PyInstaller/fake-modules'
11904 INFO: Looking for import hooks ...
11906 INFO: Processing hook   hook-distutils.py
11906 INFO: Processing hook   hook-sysconfig.py
11906 INFO: Processing hook   hook-xml.py
11907 INFO: Processing hook   hook-PIL.py
11907 INFO: Excluding import 'FixTk'
11910 WARNING:   Removing import 'FixTk'
11910 INFO: Excluding import 'Tkinter'
11910 INFO: Excluded import 'PySide' not found
11910 INFO: Processing hook   hook-httplib.py
11911 INFO: Processing hook   hook-pydoc.py
11911 INFO: Excluding import 'Tkinter'
11911 INFO: Processing hook   hook-PyQt4.py
11912 WARNING: Hidden import 'PyQt4._qt' not found (probably old hook)
11912 INFO: Processing hook   hook-encodings.py
12264 INFO: Processing hook   hook-_tkinter.py
12327 INFO: checking Tree
12327 INFO: Building Tree because out00-Tree.toc is non existent
12327 INFO: Building Tree out00-Tree.toc
12365 INFO: checking Tree
12366 INFO: Building Tree because out01-Tree.toc is non existent
12366 INFO: Building Tree out01-Tree.toc
12380 INFO: Processing hook   hook-matplotlib.backends.py
12798 INFO:   Matplotlib backend "GTK": added
13108 INFO:   Matplotlib backend "GTKAgg": added
13420 INFO:   Matplotlib backend "GTKCairo": added
13631 INFO:   Matplotlib backend "MacOSX": ignored
    cannot import name _macosx
13893 INFO:   Matplotlib backend "Qt4Agg": added
14120 INFO:   Matplotlib backend "TkAgg": added
14246 INFO:   Matplotlib backend "WX": ignored
    No module named wx
14464 INFO:   Matplotlib backend "WXAgg": ignored
    No module named wx

** (-c:4422): WARNING **: Couldn't connect to accessibility bus: Failed to connect to socket /tmp/dbus-psXhVLD3F9: Connection refused
14809 INFO:   Matplotlib backend "GTK3Cairo": added

** (-c:4423): WARNING **: Couldn't connect to accessibility bus: Failed to connect to socket /tmp/dbus-psXhVLD3F9: Connection refused
15146 INFO:   Matplotlib backend "GTK3Agg": added
15394 INFO:   Matplotlib backend "WebAgg": added
15605 INFO:   Matplotlib backend "agg": added
15819 INFO:   Matplotlib backend "cairo": added
15942 INFO:   Matplotlib backend "emf": ignored
    No module named backend_emf
16253 INFO:   Matplotlib backend "gdk": added
16467 INFO:   Matplotlib backend "pdf": added
16754 INFO:   Matplotlib backend "pgf": added
16966 INFO:   Matplotlib backend "ps": added
17181 INFO:   Matplotlib backend "svg": added
17403 INFO:   Matplotlib backend "template": added
17844 INFO: Processing pre-safe import module hook   gi.repository.GObject
18382 INFO: Processing hook   hook-PIL.Image.py
18632 INFO: Processing hook   hook-matplotlib.py
18745 INFO: Processing hook   hook-xml.dom.domreg.py
18746 INFO: Processing hook   hook-PyQt4.QtGui.py
18904 WARNING: Hidden import 'PyQt4._qt' not found (probably old hook)
18905 INFO: Processing hook   hook-pytz.py
18905 INFO: Processing hook   hook-setuptools.py
18906 INFO: Processing hook   hook-xml.sax.py
18906 INFO: Processing hook   hook-PIL.SpiderImagePlugin.py
18906 INFO: Excluded import 'FixTk' not found
18906 INFO: Excluding import 'Tkinter'
18907 INFO: Processing hook   hook-PyQt4.QtCore.py
18939 WARNING: Hidden import 'PyQT4._qt' not found (probably old hook)
18940 INFO: Processing hook   hook-xml.dom.py
18940 INFO: Processing hook   hook-gtk.py
18941 WARNING: Hidden import 'gtkglext' not found (probably old hook)
18942 WARNING: Hidden import 'gdkgl' not found (probably old hook)
18943 WARNING: Hidden import 'gdkglext' not found (probably old hook)
18943 WARNING: Hidden import 'gtk.gdk' not found (probably old hook)
18944 WARNING: Hidden import 'gtk.gtkgl' not found (probably old hook)
18944 WARNING: Hidden import 'gtk.gtkgl._gtkgl' not found (probably old hook)
18945 WARNING: Hidden import 'gtkgl' not found (probably old hook)
19008 INFO: Processing hook   hook-gi.repository.GObject.py
19076 INFO: Processing pre-safe import module hook   gi.repository.GLib
19101 WARNING: Hidden import 'gi._gobject.option' not found (probably old hook)
19101 INFO: Processing hook   hook-gi.py
19102 WARNING: Hidden import 'gi._error' not found (probably old hook)
19102 INFO: Processing hook   hook-gi.repository.GLib.py
19216 INFO: Looking for ctypes DLLs
19261 WARNING: library rpcrt4.dll required via ctypes not found
19303 INFO: Analyzing run-time hooks ...
19318 INFO: Including run-time hook 'pyi_rth_pkgres.py'
19320 INFO: Including run-time hook 'pyi_rth__tkinter.py'
19321 INFO: Including run-time hook 'pyi_rth_gi.py'
19321 INFO: Including run-time hook 'pyi_rth_qt4plugins.py'
19323 INFO: Including run-time hook 'pyi_rth_mplconfig.py'
19324 INFO: Including run-time hook 'pyi_rth_mpldata.py'
19345 INFO: Looking for dynamic libraries
21278 INFO: Looking for eggs
21278 INFO: Python library not in binary depedencies. Doing additional searching...
21314 INFO: Using Python library /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0
21347 INFO: Warnings written to /home/paugam/Src/stackimage/build/test/warntest.txt
21354 INFO: checking PYZ
21354 INFO: Building PYZ because out00-PYZ.toc is non existent
21355 INFO: Building PYZ (ZlibArchive) /home/paugam/Src/stackimage/build/test/out00-PYZ.pyz
22059 INFO: checking PKG
22059 INFO: Building PKG because out00-PKG.toc is non existent
22059 INFO: Building PKG (CArchive) out00-PKG.pkg
40123 INFO: Bootloader /usr/local/lib/python2.7/dist-packages/PyInstaller/bootloader/Linux-64bit/run
40123 INFO: checking EXE
40123 INFO: Building EXE because out00-EXE.toc is non existent
40123 INFO: Building EXE from out00-EXE.toc
40123 INFO: Appending archive to EXE /home/paugam/Src/stackimage/dist/test
最佳答案
我卸载了matplotlib并将其重新安装到1.5.1版本并且工作正常.这似乎是1.4.1版本中的一个错误.参见here.

转载注明原文:pyinstaller:使用matplotlib构建python exe - 代码日志