将.py转换为.exe,包括matplotlib代码

时间:2018-07-21 12:59:49

标签: python numpy matplotlib pyinstaller cx-freeze

我正在尝试转换此代码:

import pandas as pd
import matplotlib.pyplot as plt
import readTrc

path = 'C:/filepath/data.trc'
datX, datY, m = readTrc.readTrc(path)

srx, sry = pd.Series(datX * 1000), pd.Series(datY * 1000)
df = pd.concat([srx, sry], axis = 1)
df.set_index(0, inplace = True)

df.plot(grid = 1,
        linewidth = 0.5,
        figsize = (9,5),
        legend = False,
        xlim = (df.index[0] , df.index[-1]),
        xticks = [-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9])
plt.xlabel('Zeit in ms')
plt.ylabel('Spannung in mV')
plt.savefig('test.png', dpi = 600)

使用cx_Freeze进入可执行文件。

Setup.py:

import cx_Freeze
import sys
import matplotlib
base = None
if sys.platform == "win32":
    base = "Win32GUI"

executables = [
    cx_Freeze.Executable("read_trc.py", base = base),
    ]

build_exe_options = {"includes":["matplotlib.backends.backend_tkagg"],
                     "include_files":[(matplotlib.get_data_path(), "mpl-data"),
                                      ('C:/filepath', 'data.trc')],
                     "excludes":[],
                     }
cx_Freeze.setup(
    name = "script",
    options = {"build_exe": build_exe_options},
    version = "0.0",
    description = "A basic example",
    executables = executables)

转换有效,但是当我尝试运行.exe时,出现此错误:

enter image description here

有没有办法使这项工作有效?我正在Windows 10上使用Python 3.6。

我已经尝试过在Stackoverflow上找到的有关Numpy导入错误的修复程序,但这似乎无济于事。

编辑: 多亏了评论,我解决了错误。不幸的是,现在当我尝试执行转换后的文件时出现另一个错误:

enter image description here

Edit2:

我试图在代码中包含tkinter,但是它不起作用。我安装了标准的Python 3.6发行版,其中应包含tkinter。 tkinter.test()有效。我认为tkinter.ddls导入有问题。我该怎么做呢?

import cx_Freeze
import sys
import os
import matplotlib

os.environ['TCL_LIBRARY'] = r"C:\Users\Artur\AppData\Local\Programs\Python\Python36\DLLs\tcl86t.dll"
os.environ['TK_LIBRARY'] = r"C:\Users\Artur\AppData\Local\Programs\Python\Python36\DLLs\tk86t.dll"

base = None

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

executables = [
    cx_Freeze.Executable("plt_test.py", base = base),
    ]

include_files = [(matplotlib.get_data_path(), "mpl-data"),
                 ('C:/Users/Artur/Desktop/Nellingen/Messdaten/20180104_Zyl_NiCr_DCneg_5bar_HSC/20180104_04_Zyl_NiCr_DCneg_5bar_170kV_HSC_Firefly/C220180104_ch2_UHF00000.trc',
                  'C220180104_ch2_UHF00000.trc'),
                 (r"C:\Users\Artur\AppData\Local\Programs\Python\Python36\DLLs\tcl86t.dll",
                 r"C:\Users\Artur\AppData\Local\Programs\Python\Python36\DLLs\tk86t.dll")]

build_exe_options = {"includes":["matplotlib.backends.backend_tkagg"],
                     "include_files":include_files,
                     "excludes":[],
                     "packages":["numpy", "matplotlib", "pandas", 'tkinter', 'os'],
                     }
cx_Freeze.setup(
    name = "script",
    options = {"build_exe": build_exe_options},
    version = "0.0",
    description = "A basic example",
    executables = executables)

在Windows控制台中输出:

enter image description here

0 个答案:

没有答案