使用matplotlib

时间:2018-06-29 17:34:52

标签: python python-3.x matplotlib cx-freeze

我开始使用cx-freeze,其中源文件仅包含print("hello from cx-freeze")。一切正常。

然后,我尝试https://gist.github.com/joferkington/9214844收到了错误消息。我将源文件更改为非常简单。我仍然收到TCL_LIBRARY错误。我遵循了KeyError: 'TCL_Library' when I use cx_Freeze(马丁·图尔诺伊)的建议

错误消失了。 exe文件已创建(如500+ mb)。运行文件时,出现错误消息。

我检查其他人的问题 Creating exe file with cx_Freeze, PyQt4, matplotlib and multiple .py files does not work (此处未提供答案)

其他问题,例如cx_Freeze help. ImportError

cx-freeze fails to include modules even when included specifically 要么 cx-freeze doesn't find all dependencies

我检查了它们,但似乎不适用于我的情况(至少我不知道如何使其与我的工作相符,如果可以的话,请解释更多谢谢)

这是相似的 ImportError: No module named 'queue' while running my app freezed with cx_freeze 但是解决方案是导入多处理程序(我也尝试过它也不起作用)

我尝试了setup.py的完整选项

from cx_Freeze import setup, Executable
import sys
import os.path
PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')

base = 'Console'

options = {
    'build_exe': {'packages': ["pandas","numpy","scipy","matplotlib"],
        'include_files':[
            os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'),
            os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'),
            os.path.join(PYTHON_INSTALL_DIR, 'Library', 'plugins', 'platforms')
         ],
    },
}

executables = [
    Executable('plot2.py', base = base)
]

setup(name = "plot2" ,
      version = "0.1" ,
      description = "" ,
      executables = executables,
      options = options
      )

我也尝试较少的选择

from cx_Freeze import setup, Executable
import sys
import os.path
PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')

base = 'Console'

options = {
    'build_exe': {'packages': ["matplotlib"],
        'include_files':[
            os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'),
            os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll')]
    }
}

executables = [
    Executable('plot2.py', base = base)
]

setup(name = "plot2" ,
      version = "0.1" ,
      description = "" ,
      executables = executables,
      options = options
      )

两个都很好。我的目标文件夹中甚至有tcl86t.dlltk86t.dll。 但是当我尝试运行它时,两者都失败了。带有“较少选项”的设置会显示以下错误消息

Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\cx_Freeze\initscripts\__start
up__.py", line 14, in run
    module.run()
  File "C:\ProgramData\Anaconda3\lib\site-packages\cx_Freeze\initscripts\Console
.py", line 26, in run
    exec(code, m.__dict__)
  File "plot2.py", line 1, in <module>
  File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\__init__.py", line
 127, in <module>
    from . import cbook
  File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\cbook\__init__.py"
, line 35, in <module>
    import numpy as np
  File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\__init__.py", line 158,
 in <module>
    from . import add_newdocs
  File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\add_newdocs.py", line 1
3, in <module>
    from numpy.lib import add_newdoc
  File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\__init__.py", line
8, in <module>
    from .type_check import *
  File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\type_check.py", lin
e 11, in <module>
    import numpy.core.numeric as _nx
  File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\__init__.py", line
 38, in <module>
    from . import numeric
  File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\numeric.py", line
2896, in <module>
    from . import fromnumeric
  File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py", l
ine 15, in <module>
    from . import _methods
ImportError: cannot import name '_methods'

即使与https://github.com/anthony-tuininga/cx_Freeze/blob/master/cx_Freeze/samples/matplotlib/setup.py

base = 'Console'
if sys.platform == 'win32':
    base = 'Win32GUI'

它仍然不起作用(python setup.py build有效,但是当我双击.exe文件时该程序无法运行)。

我的plot2.py代码只是简化为创建jpg文件(它在我刚完成python plot2.py时运行

import matplotlib.pyplot as plt
x = [1,2,3,4,5.5]
y = [3,4,5,6,7.7]
plt.figure()
plt.plot(x,y,'o')
plt.savefig("test.jpg",dpi = 200)

我通过anaconda安装python。 python --version给予

Python 3.6.5 :: Anaconda, Inc.

结果。 conda list证明我的cx-Freeze版本是5.1.1

if sys.platform == 'win32':
    base = 'Win32GUI'

并在软件包列表中添加多处理器不起作用(我认为这不相关,但是看到其他人(在其他应用程序上)做到了,并且它起作用,所以我尝试了,可能是错误的)。我能做的最好的就是在运行时收到此错误(如下图所示)。

error report when click on the exe file

是否可以解决此问题?我在ubuntu系统上也有python,但是还没有尝试。如果有帮助,我可以卸载python或使用其他版本的python / cx-Freeze。如果我需要安装其他版本的python / cx-Freeze,也请告诉我该怎么做。

这里的另一个问题是,有没有一种方法可以使exe文件更小。只是创建一个图不应该是500+ mb的程序。也许我包含了应该排除的软件包/文件。请帮忙。谢谢。

1 个答案:

答案 0 :(得分:0)

我遇到了这个问题,起作用的是我使用了“ pyinstaller”而不是“ cx_freeze”,然后构建了exe文件之后,我从“ c:\ Users \\ envs \ environmentname> \复制了” platforms”文件夹库\ plugins \ platforms”到主.exe文件所在的位置。