你如何在matplotlib中使用py2app?

时间:2015-07-22 05:30:57

标签: python macos matplotlib cx-freeze py2app

我创建了一个使用matplotlib在tkinter窗口中显示绘图的应用程序。我想创建一个独立的应用程序,我一直在尝试使用py2app和cx_freez,但是当我尝试编译程序时都遇到了问题。我相信这个问题与matplotlib有关,因为当我故意禁用该模块的导入然后编译时,应用程序将启动并且只有在遇到需要matplotlib的代码时才会失败。我不确定我是否从setup.py文件中排除了某些内容。我尝试使用一些代码from here但收效甚微。

当我在终端中运行setup.py时:

from setuptools import setup

setup(
    app=["Calculator.py"],
    setup_requires=["py2app"]
)

这是我在“完成”编译后在终端中遇到的错误:

Traceback (most recent call last):
  File "setup.py", line 7, in <module>
    setup_requires=["py2app"]
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/distutils/core.py", line 148, in setup
    dist.run_commands()
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/distutils/dist.py", line 955, in run_commands
    self.run_command(cmd)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/py2app/build_app.py", line 659, in run
    self._run()
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/py2app/build_app.py", line 865, in _run
    self.run_normal()
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/py2app/build_app.py", line 959, in run_normal
    self.create_binaries(py_files, pkgdirs, extensions, loader_files)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/py2app/build_app.py", line 1214, in create_binaries
    platfiles = mm.run()
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/macholib/MachOStandalone.py", line 105, in run
    mm.run_file(fn)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/macholib/MachOGraph.py", line 84, in run_file
    self.scan_node(m)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/macholib/MachOGraph.py", line 110, in scan_node
    m = self.load_file(filename, caller=node)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/macholib/MachOGraph.py", line 93, in load_file
    newname = self.locate(name, loader=caller)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/macholib/MachOStandalone.py", line 23, in locate
    newname = super(FilteredMachOGraph, self).locate(filename, loader)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/macholib/MachOGraph.py", line 49, in locate
    loader=loader.filename)
TypeError: dyld_find() got an unexpected keyword argument 'loader'

当我尝试运行应用程序时,我在控制台中收到此错误:

2015-07-21 22:17:55.588 Calculator[85122:57708133] Calculator Error
 Traceback (most recent call last):
   File "/Users/camon/Documents/Python Apps/Compiler/dist/Calculator.app/Contents/Resources/__boot__.py", line 81, in <module>
     _run()
   File "/Users/camon/Documents/Python Apps/Compiler/dist/Calculator.app/Contents/Resources/__boot__.py", line 62, in _run
     exec(compile(source, path, 'exec'), globals(), globals())
   File "/Users/camon/Documents/Python Apps/Compiler/dist/Calculator.app/Contents/Resources/Calculator.py", line 9, in <module>
     from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
   File "/Users/camon/Documents/Python Apps/Compiler/dist/Calculator.app/Contents/Resources/lib/python3.4/matplotlib/backends/backend_tkagg.py", line 7, in <module>
     from six.moves import tkinter_filedialog as FileDialog
   File "six.pyc", line 90, in __get__
   File "six.pyc", line 113, in _resolve
   File "six.pyc", line 80, in _import_module
 ImportError: No module named 'tkinter.filedialog'

在查看错误后,我看起来在编译过程中没有添加某些模块或其他内容。我需要将什么添加到我的setup.py文件中?或者我需要用matplotlib解决这个问题?

另外......这是我程序中所有模块的列表:

import tkinter as tk
from tkinter import ttk
from math import *
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib import style
#from matplotlib import pyplot as plt
from matplotlib.figure import Figure
import random

编辑:

我发现matplotlib需要导入tkinter.filedialogtkinter.messagebox。一旦我这样做,该应用程序启动并似乎正常运行。但是,当我编译它时,我仍然在终端中得到相同的错误。 setup.py仍然缺少元素吗?

0 个答案:

没有答案