py2exe 中的 Matplotlib — ImportError:无法导入名称 dist(文件“distutils\__init__.pyc”)

2023-12-11

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

main.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 2.7.6

好吧,我没有找到解决这个问题的正确方法。

我用肮脏的黑客解决了它,只需将 venv 中的 distutils dir 替换为系统 python 的 distutils dir 即可。现在一切正常,并且在 venv 中正常工作!但不太确定它的缺点.

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

py2exe 中的 Matplotlib — ImportError:无法导入名称 dist(文件“distutils\__init__.pyc”) 的相关文章

随机推荐