Python脚本转换成exe可执行文件的方法

将py脚本打包成exe本人用过两种方法,一个是cxfreeze,一个是pyinstaller,安装后均需在cmd命令行中运行。安装包一般通过https://pypi.org/,或下载已经编译好的whl文件安装https://www.lfd.uci.edu/~gohlke/pythonlibs/,或使用https://anaconda.org/。这些安装包的方式一般需要在cmd命令行中操作,如果打开cmd命令行工具闪退,可以使用powershell超级命令行。同样打包程序也得使用命令行,首先切换到py文件所在的文件夹,再输入打包命令即可。

pyinstaller -w juxing.py

cxfreeze:在同文件夹中新建setup.py文件内如如下,路径等需要根据实际情况修改

import sys
import os
from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["os","tkinter","gdal","random"]}

os.environ['TCL_LIBRARY'] = r'D:\Anaconda3\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'D:\Anaconda3\tcl\tk8.6'

# 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 = "proj",
        version = "0.1",
        description = "My GUI application!",
        options = {"build_exe": build_exe_options},
        executables = [Executable("projt.py", base=base)])

猜你喜欢

转载自blog.csdn.net/weixin_40450867/article/details/81184402