distutils脚本实现--Python使用cx_Freeze编译可执行文件(exe,mac)

简述

比如说,我这里有一个程序,test.py需要变成exe文件

test.py文件内容如下:

import os
print("Hello World!")
os.system("pause")

使用方法

  • 首先,你需要有创建一个distutils脚本(内容如下)
  • 其实一堆废话来的,关键的地方就是在setup中的内容上了。
  • name的话,就是建立的项目名字(随便取就好了)
  • verison是设置的版本
  • description是描述
  • (其实上面这个三个玩意影响不是很大。。。随便写写就好了)
  • options 是重点,内容为字典,比如,这里需要变成在windows下的可执行程序,那么这里当然就是exe啦!
  • 然后,这个option,在上面也可以看到,就把对应的包的名字放进去,左边放的是系统自带包,右边是自扩展包
  • 然后最后面就是executables中要封装一个类来,类中放文件名,就是这个test.py
  • 然后在命令行下输入python setup.py build 就ok了
  • 之后在文件夹中就会有多一个build文件夹里面有exe文件
import sys
from cx_Freeze import setup, Executable

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

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

猜你喜欢

转载自blog.csdn.net/a19990412/article/details/80955616