吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 9658|回复: 50
收起左侧

[Python 转载] Python 打包工具 Nuitka 简明教程

   关闭 [复制链接]
beavailable 发表于 2021-12-7 20:08
本帖最后由 beavailable 于 2022-1-16 18:36 编辑

不多说,让我们直奔主题吧,本教程一共5个步骤,看完你就会了。

第0步:准备工作

这一步只针对Windows,其他系统可以忽略。

安装 mingw-w64-gcc

这是一个编译器(打包必需),当然你用msvc也可以,这里我就使用mingw-w64-gcc了。  

下载地址(安装及配置环境变量的过程略):
https://winlibs.com/

第1步:创建一个 python 项目

For Windows

创建一个文件夹demo,打开cmd并进入demo目录后执行如下命令:

python -m venv env
env\Scripts\activate

For Linux

mkdir demo
cd demo
python3 -m venv env
. env/bin/activate

第2步:编写 main.py

内容如下:

print('ojbk')

第3步:安装 nuitka

nuitka 就是我们用来打包的工具

pip install nuitka

第4步:开始打包

普通打包

For Windows

nuitka --standalone --remove-output --mingw64 main.py

For Linux

nuitka3 --standalone --remove-output main.py

提示:
如果在执行命令的过程中遇到Yes/No的问题,直接输入Yes按回车就行了。
Linux用户在执行命令时可能会遇到报错,如果是缺少依赖,按照提示安装对应的依赖即可。

执行完上述命令后,你应该能在项目目录下看到一个名为main.dist的文件夹,这就是我们需要的东西了。

单文件打包

单文件打包相比普通打包,只需再添加一个选项即可:

--onefile

作为一个可选步骤,安装zstandard后再打包可以让打包出来的文件更小:

pip install zstandard

没错,只要安装zstandard就可以了,无需额外的工作,直接按照上面的方法打包就行。

更多功能

参见--help

免费评分

参与人数 6吾爱币 +6 热心值 +6 收起 理由
sygde + 1 + 1 谢谢@Thanks!
ez000016 + 1 + 1 谢谢@Thanks!
dtsuifeng + 1 + 1 用心讨论,共获提升!
shizi1521 + 1 + 1 谢谢@Thanks!
yjn866y + 1 + 1 我很赞同!
Pandolar + 1 + 1 我很赞同!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| beavailable 发表于 2021-12-8 08:13
shojnhv 发表于 2021-12-8 07:31
和pyInstaller,cxFreeze比有什么不同?

nuitka 不仅是一个打包工具,更是一个编译器,它把 py 文件编译成 c 文件,再调用 c 编译器编译成二进制程序,性能更好。
大兵马元帅 发表于 2021-12-8 00:59
水墨青云 发表于 2022-2-11 08:22
beavailable 发表于 2022-2-11 07:58
gcc 环境变量没有配置好

系统环境变量里PATH添加了C:\mingw64\bin  ,cmd里gcc -v会显示数据,但打包时一直会有那个找不到路径。后来我把mingw64都删了,让nuitka自己下mingw64在C:\Users\***\AppData\Local\Nuitka\Nuitka\gcc\x86_64\11.2.0-12.0.1-9.0.0-r1里,打包都正常打包,也报找不到路径。然后我发现不加--standalone 生成的一个小的exe能正常运行,但是加了以后在dist里的exe不能正常运行,提示被另一个程序占用。
[Asm] 纯文本查看 复制代码
PS D:\Users\*\Documents\code\python> nuitka --mingw64 --show-progress --output-dir=out 爱奇艺签到.py
Nuitka-Options:INFO: Used command line options: --mingw64 --show-progress --output-dir=out 爱奇艺签到.py
[color=Red]Nuitka-Options:WARNING: You did not specify to follow or include anything but main program. Check options and make sure that is intended.[/color]
Nuitka:INFO: Starting Python compilation with Nuitka '0.6.19.6' on Python '3.10' commercial None.
Nuitka-Progress:INFO: PASS 1:
Nuitka-Progress:INFO: Optimizing module '__main__', 0 more modules to go after that.
Nuitka-Progress:INFO: Doing module local optimizations for '__main__'.
Nuitka-Progress:INFO: Not finished with the module due to following change kinds: new_constant,new_expression,new_statements
Nuitka-Progress:INFO: Not finished with the module due to following change kinds: loop_analysis,new_builtin,new_builtin_ref,new_constant,new_expression,new_statements,var_usage
Nuitka-Progress:INFO: Not finished with the module due to following change kinds: loop_analysis,new_builtin,new_constant,new_expression,new_statements,var_usage
Nuitka-Progress:INFO: Not finished with the module due to following change kinds: loop_analysis
Nuitka-Progress:INFO: Not finished with the module due to following change kinds: loop_analysis
Nuitka-Progress:INFO: No changed, but retrying one more time.
Nuitka-Progress:INFO: Finished with the module.
Nuitka-Progress:INFO: PASS 2:
Nuitka-Progress:INFO: Optimizing module '__main__', 0 more modules to go after that.
Nuitka-Progress:INFO: Doing module local optimizations for '__main__'.
Nuitka-Progress:INFO: Finished with the module.
Nuitka:INFO: Completed Python level compilation and optimization.
Nuitka:INFO: Generating source code for C backend compiler.
Nuitka:INFO: Total memory usage before generating C code: 32.62 MB (34201600 bytes):
Nuitka:INFO: Total memory usage before running scons: 37.65 MB (39477248 bytes):
Nuitka:INFO: Running data composer tool for optimal constant value handling.
Nuitka:INFO: Running C compilation via Scons.
Nuitka-Scons:INFO: Backend C compiler: gcc (gcc).
系统找不到指定的路径。
make: [D:\Users\*\TEMP\ccG823oV.ltrans9.ltrans.o] Error 1 (ignored)
系统找不到指定的路径。
make: [D:\Users\*\TEMP\ccG823oV.ltrans5.ltrans.o] Error 1 (ignored)
系统找不到指定的路径。
make: [D:\Users\*\TEMP\ccG823oV.ltrans2.ltrans.o] Error 1 (ignored)
系统找不到指定的路径。
make: [D:\Users\*\TEMP\ccG823oV.ltrans8.ltrans.o] Error 1 (ignored)
系统找不到指定的路径。
make: [D:\Users\*\TEMP\ccG823oV.ltrans11.ltrans.o] Error 1 (ignored)
系统找不到指定的路径。
make: [D:\Users\*\TEMP\ccG823oV.ltrans10.ltrans.o] Error 1 (ignored)
系统找不到指定的路径。
make: [D:\Users\*\TEMP\ccG823oV.ltrans0.ltrans.o] Error 1 (ignored)
系统找不到指定的路径。
make: [D:\Users\*\TEMP\ccG823oV.ltrans7.ltrans.o] Error 1 (ignored)
系统找不到指定的路径。
make: [D:\Users\*\TEMP\ccG823oV.ltrans6.ltrans.o] Error 1 (ignored)
系统找不到指定的路径。
make: [D:\Users\*\TEMP\ccG823oV.ltrans1.ltrans.o] Error 1 (ignored)
系统找不到指定的路径。
make: [D:\Users\*\TEMP\ccG823oV.ltrans4.ltrans.o] Error 1 (ignored)
系统找不到指定的路径。
make: [D:\Users\*\TEMP\ccG823oV.ltrans3.ltrans.o] Error 1 (ignored)
Nuitka-Scons:INFO: Compiled 10 C files using ccache.
Nuitka-Scons:INFO: Cached C files (using ccache) with result 'cache miss': 10
Nuitka:INFO: Keeping build directory 'out\\爱奇艺签到.build'.
Nuitka:INFO: Successfully created 'out\\爱奇艺签到.exe'.
龍謹 发表于 2021-12-8 06:55
正在学习PY,打包的方法先收藏,谢谢分享!
shojnhv 发表于 2021-12-8 07:31
和pyInstaller,cxFreeze比有什么不同?
xiaoshu1688 发表于 2021-12-8 07:47
挺详细的,感谢。
 楼主| beavailable 发表于 2021-12-8 07:54
大兵马元帅 发表于 2021-12-8 00:59
Python打包后文件太大了

压缩后也可以很小的,看我这个项目:
https://github.com/beavailable/share/releases
只有 8.5M
Pandolar 发表于 2021-12-8 08:05
和pyInstaller比的优势在哪呢
 楼主| beavailable 发表于 2021-12-8 08:20
Pandolar 发表于 2021-12-8 08:05
和pyInstaller比的优势在哪呢

见置顶回复
lbbas 发表于 2021-12-8 08:55
启动速度和体积上都有优势,就是感觉编译配置啥的有点复杂,尤其在各种外部库的情况下,pyinstaller之前针对部分库做了打包优化,不知道nuitka现在进展如何。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-3 13:21

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表