换个方式针对
python
脚本进行打包, 有些时候我们写的一些脚本里面可能会包含SECRET
或PASSWORD
相关的信息,但是这些脚本有的时候是需要给到其他部门的小伙伴使用,这个时候如何能保障账号密码的安全性呢(当然了,反编译还是防不住的), 获取之前你使用过pyinstaller
解决过类似问题,今天我们要说的并不是pyinstaller
, 而是nuitka
。
Nuitka is a Python compiler written in Python. It's fully compatible with Python 2.6, 2.7, 3.3, 3.4, 3.5, 3.6, 3.7, and 3.8. You feed it your Python app, it does a lot of clever things, and spits out an executable or extension module.
Nuitka
是用Python
编写的Python
编译器。支持Python
2.6、2.7、3.3、3.4、3.5、3.6、3.7和3.8等版本。将你的python
程序打包成一个可执行文件。
Nuitka对应的github地址[1]
Nuitka官网[2]
nuitka
和pyinstaller
一样,也是支持生产单文件和目录形式的,如果没有指定,那就是目录形式的,所以要特别注意~
复杂的程序进行打包的时候耗时是比较久的,打包速度这点比不上pyinstaller
生成的二进制文件大小比较大,这也算是一个不好的点吧~
# 这是官方文档提供的代码片段
# cat main.py
def talk(message):
return "Talk " + message
def main():
print( talk("Hello World"))
if __name__ == "__main__":
main()
pip install nuitka
python -m nuitka --standalone main.py
# 执行结果差异如下
hello-world-demo: python main.py
Talk Hello World
hello-world-demo: ./main.dist/main
Talk Hello World
hello-world-demo:
原始代码我就不贴了,功能是用来添加阿里云站点监控功能的,之前尝试使用pyinstaller
来进行打包,但是么有成功,失败的原因是因为aliyunsdkcore
依赖了etry_config.json
文件, 而pyinstaller
并不能去获取到,我尝试添加目录也没有成功,今天试下nuitka
看看是否能够成功。
# 打包前执行效果
aliyun-alert(master) ✗: python alertadd.py www.baidu.com
{"RequestId":"D1DD68D4-7D93-2C94-A362-DF9F1E8C46F4","Data":"29884879351CF69959852ABFC269EFEB26564237","Code":"200","Success":true}
{"RequestId":"048AF305-EC0A-4D3A-8722-C6D2FA163E46","Data":"7A2E57F9E7D1FF3ED242163231A403C526564237","Code":"200","Success":true}
# 打包
python -m nuitka --follow-imports --include-plugin-directory=/aliyun-alert alertadd.py
# 打包后执行效果
aliyun-alert(master) ✗: ./alertadd.bin www.baidu.com
{"RequestId":"E8D95559-9133-4A5E-AEC0-70581DB87A21","Data":"D64121A091FB85A818DB42268E8B5D3F26564254","Code":"200","Success":true}
aliyun-alert(master) ✗:
nuitka
工具的出色程度超出了我的预料(同样也是支持win
的),哈哈,是真的香甜可口,除了我们前面提到的工具之后还有什么其他的么?同类工具对比图片来源[3]
刚开始看到nuitka
的介绍并没有觉得这个工具会比pyinstaller
更好,或者更牛逼,因为个人在使用pyinstaller
的时候并不如预期,也有可能我使用姿势不太对,真正的是尝试使用了下,发现还是先入为主的思维害了自己,用着还行~
[1]
Nuitka对应的github地址: https://github.com/Nuitka/Nuitka
[2]
Nuitka官网: http://nuitka.net/
[3]
同类工具对比图片来源: https://docs.python-guide.org/shipping/freezing/