python程序打包新姿式(nuitka)
換個方式針對
python
腳本進行打包, 有些時候咱們寫的一些腳本里面可能會包含SECRET
或PASSWORD
相關的信息,可是這些腳本有的時候是須要給到其餘部門的小夥伴使用,這個時候如何能保障帳號密碼的安全性呢(固然了,反編譯仍是防不住的), 獲取以前你使用過pyinstaller
解決過相似問題,今天咱們要說的並非pyinstaller
, 而是nuitka
。python
關於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.git
Nuitka
是用Python
編寫的Python
編譯器。支持Python
2.六、2.七、3.三、3.四、3.五、3.六、3.7和3.8等版本。將你的python
程序打包成一個可執行文件。github
官網和github
Nuitka對應的github地址[1]json
Nuitka官網[2]安全
說明
單文件或目錄
nuitka
和pyinstaller
同樣,也是支持生產單文件和目錄形式的,若是沒有指定,那就是目錄形式的,因此要特別注意~微信
執行時間
複雜的程序進行打包的時候耗時是比較久的,打包速度這點比不上pyinstaller
app
產物大小
生成的二進制文件大小比較大,這也算是一個很差的點吧~ide
實戰
配套腳本
# 這是官方文檔提供的代碼片斷# cat main.pydef 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.pyTalk Hello World
hello-world-demo: ./main.dist/mainTalk Hello Worldhello-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]flex

總結
剛開始看到nuitka
的介紹並無以爲這個工具會比pyinstaller
更好,或者更牛逼,由於我的在使用pyinstaller
的時候並不如預期,也有可能我使用姿式不太對,真正的是嘗試使用了下,發現仍是先入爲主的思惟害了本身,用着還行~
歡迎關注個人公衆號「追馬Linux」,原創技術文章第一時間推送。

引用連接
[1]
Nuitka對應的github地址: https://github.com/Nuitka/Nuitka[2]
Nuitka官網: http://nuitka.net/[3]
同類工具對比圖片來源: https://docs.python-guide.org/shipping/freezing/
本文分享自微信公衆號 - 追馬Linux(zhuima_k8s)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。