Simon Logo
WritingMonthlyProjects
Simon Logo

Thanks for reading!

CC BY-NC-SA 4.0© 2019-2026 Simon Wong.

Contact

GitHubGithubXTwitterZhihu知乎WeChat微信公众号RSSRSS

Legal

隐私策略服务条款
CC BY-NC-SA 4.0© 2019-2026 Simon Wong.

On this page

On this page

PythonPyInstallerNuitka打包构建可执行文件

把 Python 代码构建成可执行文件

Nov 27, 2024

创建打包环境

以 Conda 为例创建环境

Conda 可以为你的应用创建一个单独的环境,可以避免不必要的包被打包,减少包的体积大小.

创建环境:

shell
# 创建新环境
conda create -n build_to_binary python=3.9 -y

# 激活新环境
conda activate build_to_binary

安装依赖:

shell
# 安装 PyInstaller
pip install pyinstaller
# 或者 Nuitka
pip install nuitka

# 安装其他的依赖
pip install xxx
0
0
1
2
0
4
Views
Previous

Eslint Flat Config for Typescript & React

Next

IP 类型选择指南:IDC 机房 IP、ISP IP、住宅 IP 哪个适合你?

You Might Also Like

  • 用 Claude Code 写年终汇报:数据分析到 PPT 一条龙Jan 02, 2026

使用 PyInstaller

PyInstaller 打包成一个文件夹

文件夹模式便于分发和调试。运行速度还可以。

shell
pyinstaller \
  --onedir \
  --name=app-name \
  --clean \
  main.py

PyInstaller 打包成一个文件

单文件模式启动较慢。

shell
pyinstaller \
  --onefile \
  --name=app-name \
  --clean \
  main.py

使用 Nuitka

Nuitka 打包成一个文件夹

第一次运行会比较慢,后来都很快。

shell
nuitka \
  --standalone \
  --follow-imports \
  --show-progress \
  --output-dir=dist \
  --output-filename=app-name \
  --remove-output \
  main.py

Nuitka 打包成一个文件

跟 PyInstaller 的 onefile 一样,启动都是很慢的。

shell
nuitka \
  --onefile \
  --standalone \
  --follow-imports \
  --output-dir=dist \
  --output-filename=app-name \
  --remove-output \
  main.py