版本比较
Nuitka-好用的 Python 打包工具
正文
增加 14 行删除 14 行
47 行未改动484950515252535455555657585910 行未改动707172737474757677777879808181828384858687888990909192939495969798999910010110210326 行未改动13013113213313413413513613713813813914014014114214314414414514614714814915015115115215315415515615715815916016016116216216316416516625 行未改动47 行未改动import sys
def build_with_nuitka(): """使用Nuitka进行更优化的打包,生成更小的exe文件"""
# 获取当前目录 current_dir = os.path.dirname(os.path.abspath(__file__))
# Nuitka命令参数 - 简化版本 cmd = [ sys.executable, '-m', 'nuitka',10 行未改动 '--assume-yes-for-downloads', # 自动下载依赖 # '--quiet', # 静默模式,减少输出 'main.py' ]
print("正在使用Nuitka进行优化打包...") print(f"命令: {' '.join(cmd)}")
try: # 执行打包命令 result = subprocess.run(cmd, cwd=current_dir, capture_output=True, text=True)
if result.returncode == 0: print("✅ Nuitka打包成功!") print(f"生成的exe文件位置: {os.path.join(current_dir, 'dist_nuitka', 'main.exe')}") print("文件大小应该比PyInstaller打包的小30-50%") else: print("❌ Nuitka打包失败!") print("错误信息:") print(result.stderr)
except FileNotFoundError: print("❌ 未找到Nuitka,请先安装:") print("pip install nuitka") except Exception as e: print(f"❌ 打包过程中出现错误: {e}")
def build_with_cx_freeze(): """使用cx_Freeze进行打包"""
# cx_Freeze setup脚本内容 setup_content = '''import sysimport os26 行未改动 options={"build_exe": build_exe_options}, executables=[Executable("main.py", base=base)])'''
# 写入setup脚本 with open('setup_cxfreeze.py', 'w', encoding='utf-8') as f: f.write(setup_content)
cmd = [sys.executable, 'setup_cxfreeze.py', 'build']
print("正在使用cx_Freeze进行打包...") try: result = subprocess.run(cmd, cwd=current_dir, capture_output=True, text=True)
if result.returncode == 0: print("✅ cx_Freeze打包成功!") print(f"生成的exe文件位置: {os.path.join(current_dir, 'dist_cxfreeze')}") else: print("❌ cx_Freeze打包失败!") print(result.stderr)
except FileNotFoundError: print("❌ 未找到cx_Freeze,请先安装:") print("pip install cx_Freeze")
if __name__ == "__main__": print("请选择打包方式:") print("1. Nuitka (推荐,文件更小)") print("2. cx_Freeze")
choice = input("请输入选择 (1或2): ").strip()
if choice == "1": build_with_nuitka() elif choice == "2": build_with_cx_freeze()25 行未改动