【问题标题】:Matplotlib in py2exe — ImportError: cannot import name dist (File "distutils\__init__.pyc")py2exe 中的 Matplotlib — ImportError: cannot import name dist (File "distutils\__init__.pyc")
【发布时间】:2014-08-11 05:30:47
【问题描述】:

Matplotlib 在此应用程序中完美运行.. 但由于某种原因无法在构建中运行。为什么?

我很乐意接受任何可以帮助我的建议。

.exe.log

Traceback (most recent call last):
  File "main.py", line 3, in <module>
  File "zipextimporter.pyc", line 82, in load_module
  File "matplotlib\__init__.pyc", line 103, in <module>
  File "zipextimporter.pyc", line 82, in load_module
  File "distutils\__init__.pyc", line 25, in <module>
ImportError: cannot import name dist

ma​​in.py 是我正在构建的脚本。第 3 行:

import matplotlib

build.py

# encoding: utf-8

import os
import sys
import errno

sys.path.append(os.path.abspath("."))

from distutils.core import setup
import shutil
import py2exe
import matplotlib as mpl

mpl.use('Agg')

distDir = 'dist'
shutil.rmtree('build', ignore_errors=True)
shutil.rmtree(distDir, ignore_errors=True)

try:
    os.makedirs(distDir)
except OSError as exc:
    if exc.errno == errno.EEXIST and os.path.isdir(distDir):
        pass
    else:
        raise

icon = 'icon.ico'

includes = ['matplotlib', 'numpy']
packages = ['matplotlib', 'pytz']
excludes = [
    '_gtkagg', '_tkagg', 'bsddb', 'curses', 'email',
    'pywin.debugger', 'pywin.debugger.dbgcon', 'pywin.dialogs',
    'tcl', 'Tkconstants', 'Tkinter', 'sqlite3', 'doctest', 'test'
]
dll_excludes = [
    'libgdk-win32-2.0-0.dll', 'libgobject-2.0-0.dll', 'libgdk_pixbuf-2.0-0.dll',
    'tcl84.dll', 'tk84.dll', 'w9xpopen.exe'
]
data_files = mpl.get_py2exe_datafiles()


class Target(object):
    def __init__(self, **kw):
        self.__dict__.update(kw)


icon_resources = [(0, icon)]
GUI2Exe_Target = Target(
    script='main.py',
    dest_base='app_name',
    name='app_name',
    company_name='company_name',
    copyright='company_name',
    version='0.0.1',
    icon_resources=icon_resources,
    bitmap_resources=[],
    other_resources=[]
)

setup(
    options={
        "py2exe": {
            "compressed": 1,
            "optimize": 0,
            "includes": includes,
            "excludes": excludes,
            "packages": packages,
            "dll_excludes": dll_excludes,
            "bundle_files": 1,
            "dist_dir": distDir,
            "skip_archive": False,
            "xref": False,
            "ascii": False,
            "custom_boot_script": '',
        }
    },
    zipfile=None,
    data_files=data_files,
    console=[],
    windows=[GUI2Exe_Target],
    service=[],
    com_server=[],
    ctypes_com_server=[]
)

点冻结

..
matplotlib==1.3.1
numpy==1.8.2
..

python --version

Python 2.7.6

【问题讨论】:

  • distutils 模块是否安装在 python 中?
  • @YogeeshSeralathan 是的,是的。我刚刚检查过了。我可以在当前的 virtualenvwrapper 环境中from distutils import dist

标签: python matplotlib py2exe importerror distutils


【解决方案1】:

好的,我没有找到适合这个问题的解决方案。

我用脏黑客解决了这个问题,只需将 venv 中的 distutils 目录替换为系统 python 的 distutils 目录即可。现在一切正常,并且在 venv 中工作! 不太确定这样做的缺点

据我所知,问题是 venv 的 distutils 真的很奇怪。好像 venvwrapper 或/和 python 包由于某些原因改变了它,我不知道。

如果您对这种情况有所了解,请继续并将其作为答案或 cmets 添加到线程中。 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-22
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    • 2016-05-16
    • 2018-10-27
    • 2020-07-16
    相关资源
    最近更新 更多