当我通过以下命令制作exe文件时
pyinstaller --hidden-import=pkg_resources.py2_warn --onefile example.py
它工作正常,并将我的python脚本转换为.exe,但是当我将该文件复制到其他位置时,它不起作用并说无法打开自我错误,然后我尝试了以下尝试,但均失败了
我已将.exe中所需的所有图标复制到同一文件夹中
尝试1)
pyinstaller --debug --onefile --noupx test.py#same error
尝试2)
错误(试图复制错误,但由于控制台保留了不到一秒的时间并且被自动关闭而无法这样做)
cannotopenself (then it gives the location where I copied my newly made .exe file)
然后我尝试使用CX_FREEZE然后再次说没有tkinter模块发现错误
我已经在脚本所在的主文件中包含了所有必需的DLL文件,但仍给tkinter not found错误,尝试了以下方法
我已经包含了tkinter但仍然给出此错误
这是我的setup.py文件
from cx_Freeze import setup, Executable
# imported tkinter here also
from tkinter import *
import sys
import os.path
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')
# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["os"], 'includes':["tkinter"],'include_files':['tk86t.dll','tcl86t.dll','text.ico','viewer.ico']}
# included tkinter also
# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup( name = "MINE",
version = "0.1",
description = "MY FILE",
options = {"build_exe": build_exe_options},
executables = [Executable("EXAMPLE.py", base=base)])
现在,我如下更改了Setup.py脚本,并将文件夹名称从Tkinter更改为tkinter,现在它说
from cx_Freeze import setup, Executable
from tkinter import *
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')
# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["os","numpy","time","optparse","linecache","pandas",
"matplotlib","PIL","tk"],'include_files':['tcl86t.dll','tk86t.dll','graphs.ico','viewer.ico']}
# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup( name = "My file",
version = "0.1",
description = "MY FILE ",
options = {"build_exe": build_exe_options},
executables = [Executable("My_file.py", base=base)])
现在说
这是我们需要使用cx_freeze遵循的方法
a)首先从安装了python的python文件夹中复制tk86t.dll,tcl86t.dll,通常它在本地文件夹的app_data中,然后将其复制到您的setup.py文件中
b)然后通过安装在pycharm终端类型 pip install cx_freeze
c)您需要在setup.py文件的构建选项中安装main.py文件中包含的所有库,然后python setup.py build
在pycharm终端中键入
以下是tan_an在评论中建议的setup.py文件,我要求他,但他没有发布答案,因此对我正在帮助的社区有所帮助
from cx_Freeze import setup, Executable
from tkinter import *
import sys
import os.path
import mpl_toolkits
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')
# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["os","numpy","time","optparse","linecache","pandas",
"matplotlib","PIL","tk"],"namespace_packages":['mpl_toolkits'],'include_files':['tcl86t.dll','tk86t.dll']}
# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup( name = "Your file",
version = "0.1",
description = "your file",
options = {"build_exe": build_exe_options},
executables = [Executable("yourfile.py", base=base)])
现在,在构建之后转到build文件夹,在win32文件夹中转到lib那里,然后将Tkinter文件夹的名称更改为tkinter现在运行您的应用程序
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句