One . pyinstaller and Nuitka Use feeling
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 .
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 .
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 .
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