程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Pythons packaging artifact - nuitka

編輯:Python

One . pyinstaller and Nuitka Use feeling

1.1 Use requirements

This time, it is also due to the needs of the project , To put python The code of is converted to exe The program , After a long search , Found out 2 All right python Tools for project packaging ——pyintaller and nuitka.

this 2 Both tools can meet the needs of the project at the same time :

  • Hidden source code . there pyinstaller By setting key To encrypt the source code ; and nuitka Will be python Source code into C++( What we get here is binary pyd file , Prevents decompilation ), Then compile it into an executable file .

  • Easy to transplant . Easy to use , No more installation python ah , Third party packages and so on .

1.2 Use feeling

2 The biggest feeling after using a tool is :

  • pyinstaller Experience is very poor !

    • An in-depth learning project finally turned into exe It's close 3 individual G Size (pyinstaller Is to package the entire running environment ), Yes , You heard me , One EXE Yes 3 individual G!

    • Packaging is super slow , Super slow start .

  • nuitka It's delicious !

    • The same project , Generated exe Only 7M!

    • Pack super fast (1min within ), Start super fast .

Two . Nuitka Installation and use of

2.1 nuitka Installation

  • Direct use of pip You can install :pip install Nuitka

  • download vs2019(MSVS) perhaps MinGW64, Anyway, it's all C++ The compiler , Make yourself at home .

2.2 Using process

For projects that rely on more packages from third parties ( Such as the need to import torch,tensorflow,cv2,numpy,pandas,geopy wait ) for , The best way to package here is to just turn your own code into C++, Regardless of these large third-party packages !

Below is me. demo A directory structure of ( It's used here pytq5 The interface written by the framework ):

├─utils// Source code 1 Folder ├─src// Source code 2
Folder ├─logo.ico//demo The icon └─demo.py//main file

Use the following command ( debugging ) Directly generate exe file :

nuitka --standalone --show-memory --show-progress --nofollow-imports --plugin-enable=qt-plugins --follow-import-to=utils,src --output-dir=out --windows-icon-from-ico=./logo.ico demo.py

Here is a brief introduction to my above nuitka The order of :

  • --standalone: Easy to transplant to other machines , No more installation python

  • --show-memory --show-progress: Show the progress of the whole installation

  • --nofollow-imports: Do not compile all of the import, such as keras,numpy And so on. .

  • --plugin-enable=qt-plugins: I use... Here pyqt5 To make the interface , here nuitka There are corresponding plug-ins .

  • --follow-import-to=utils,src: It needs to be compiled into C++ The code specifies 2 A folder containing the source code , Here we use , To separate .

  • --output-dir=out: Specify the output result path as out.

  • --windows-icon-from-ico=./logo.ico: Specify the generated exe The icon of is logo.ico This icon , Here is a recommended way to turn the picture into ico Format file website ( Bitworm ).

  • --windows-disable-console: function exe Cancel the box . We didn't put it here because we still need to debug , There may be some problems .

after 1min After compiling , You can see it in your directory :

├─utils// Source code 1 Folder ├─src// Source code 2 Folder ├─out// Generated exe Folder 
├─demo.build
└─demo.dist
└─demo.exe// Generated exe file ├─logo.ico//demo The icon └─demo.py//main file

Of course here you will find that the real operation exe When , Will report a mistake :no module named torch,cv2,tensorflow Wait, these are not converted into C++ Third party package for .

Here we need to find these bags ( I was in software\python3.7\Lib\site-packages Next ) Copy ( such as numpy,cv2 This folder ) To demo.dist Under the path .

thus ,exe It works perfectly !

【python Study 】
learn Python The partners , Welcome to join the new exchange 【 Junyang 】:1020465983
Discuss programming knowledge together , Become a great God , There are also software installation packages in the group , Practical cases 、 Learning materials


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved