我正在Spyder / Anaconda的Win 7上编程。而且我在将py转换为exe时遇到了麻烦。对于背景,我的程序.py有一些csv文件,它从中获取数据,要求用户输入4个整数,并在matplotlib中生成图。它将在下面导入软件包。
我能够执行类似(How can I convert a .py to .exe for Python?)的操作,但是当我开始使用自己的代码时,我的情况不起作用。
如果我在软件包列表中包含“ matplotlib”,则会得到“ KeyError:'TCL_Library”。这是什么错误,我该如何解决?添加“ os”可以作为参考。
在我的程序py中,我使用:从os导入listdir导入os,从pylab导入matplotlib.pyplot作为plt,将numpy导入np,导入matplotlib,随机导入。我是将这些保留在程序py中还是将其移动到设置中,以及如何在程序包数组中包含“ from xxx”项?
import os
from cx_Freeze import setup, Executable
base = None
executables = [Executable("try1.py", base=base)]
cwd = os.getcwd()
f_3_to_3=cwd+'\\' + '3_to_3.csv'
packages = ["idna", "matplotlib"]
options = {
'build_exe': {
"include_files": (f_3_to_3),
'packages':packages,
},
}
setup(
name = "FirstBuild",
options = options,
version = "0",
description = 'This is cool',
executables = executables
)
答案 0 :(得分:0)
将其用于我的安装文件即可。注意,我必须修复tk和tcl的目录并重新安装它们
import os
from cx_Freeze import setup, Executable
import sys
base = None
os.environ['TCL_LIBRARY'] = "C:\\ProgramData\\Anaconda3\\Library\\lib\\tcl8.6"
os.environ['TK_LIBRARY'] = "C:\\ProgramData\\Anaconda3\\Library\\lib\\tk8.6"
executables = [Executable("MyPyFile.py", base=base)]
packages = ["idna", "os", "numpy", "numpy.core._methods", "matplotlib", "random"]
options = {
'build_exe': {
"includes": ["numpy.core._methods", "numpy", "tkinter"],
"include_files": [r'C:\ProgramData\Anaconda3\Library\plugins\platforms'],
'packages':packages,
},
}
setup(
name = "FirstBuild",
options = options,
version = "0",
description = 'This is cool',
executables = executables
)