Matplotlib in py2exe — ImportError: cannot import name dist (File “distutils\__init__.pyc”)

微笑、不失礼 提交于 2021-02-18 12:09:43

问题


Matplotlib working in this app perfectly.. but not working in build for some reason. Why?

I'll gladly take any advice that can help me.

.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 is a script that I'm building. line #3 of it:

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=[]
)

pip freeze:

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

python --version:

Python 2.7.6

回答1:


Okay, I did not find proper solution for this problem.

I solved it with dirty hack, by simply replacing distutils dir in venv by distutils dir of system python. Now it all working and it working in venv! Don't quite sure about drawbacks of that though.

The issue as I can understand it, is that distutils of venv is really weird thing. Seems like venvwrapper or/and python packages changed it for some reasons, I don't know.

If you know something about this situation, please go ahead and add it to the thread as answers or comments. :)



来源:https://stackoverflow.com/questions/25236377/matplotlib-in-py2exe-importerror-cannot-import-name-dist-file-distutils-i

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