click on blue
picture
Follow us

From the Internet, intrusion and deletion

1. Experience with pyinstaller and Nuitka

1.1 Usage Requirements

This time, due to the needs of the project, to convert the python code into an exe program, after searching for a long time, I found two tools that can package python projects - pyintaller and nuitka.

These two tools can meet the needs of the project at the same time:

  • Hide source code . The pyinstaller here encrypts the source code by setting the key; while nuitka converts the python source code into C++ (the binary pyd file is obtained here, which prevents decompilation), and then compiles it into an executable file.

  • Easy to transplant . It is easy for users to use, and there is no need to install any python, third-party packages and the like.

1.2 User experience

The biggest feeling after using the two tools is:

  • Bad experience with pyinstaller!

    • The exe finally converted from a deep learning project has a size of nearly 3 G (pyinstaller packages the entire runtime environment), yes, you heard right, an EXE has 3 G!

    • Packaging is super slow, and startup is super slow.

  • nuitka is so fragrant!

    • The same project, the generated exe is only 7M!

    • The packaging is super fast (within 1min), and the startup is super fast.

2. Installation and use of Nuitka

2.1 Installation of nuitka

  • You can install it directly with pip:pip install Nuitka

  • Download vs2019 (MSVS) or MinGW64, they are all C++ compilers anyway, just download them.

2.2 Use process

For projects with many third-party dependent packages (such as import torch, tensorflow, cv2, numpy, pandas, geopy, etc.), the best way to package here is to only convert your own code into C++, regardless of these large 3rd party package!

The following is a directory structure of my demo (the interface written by the pytq5 framework is used here):

├─utils//源码1文件夹├─src//源码2文件夹├─logo.ico//demo的图标└─demo.py//main文件

Use the following command (debug) to directly generate the 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 the command of nuitka above:

  • --standalone: Easy to port to other machines, no need to install python

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

  • --nofollow-imports: Do not compile all imports in the code, such as keras, numpy and the like.

  • --plugin-enable=qt-plugins: I use pyqt5 for the interface here, and nuitka has its corresponding plug-in.

  • --follow-import-to=utils,src: The two designated folders containing source code that need to be compiled into C++ code are ,used for separation.

  • --output-dir=out: The result path of the specified output is out.

  • --windows-icon-from-ico=./logo.ico: Specify the icon of the generated exe as the icon of logo.ico. Here we recommend a website (bitworm) that converts pictures into ico format files.

  • --windows-disable-console: Run the exe to cancel the popup. I didn't put it here because we still need to debug, and there may be problems.

After 1min of compilation, you can see in your directory:

├─utils//源码1文件夹├─src//源码2文件夹├─out//生成的exe文件夹   
   ├─demo.build   
   └─demo.dist 
             └─demo.exe//生成的exe文件├─logo.ico//demo的图标└─demo.py//main文件

Of course, here you will find that when you actually run the exe, it will report an error: no module named torch,cv2,tensorflowwait for these third-party packages that are not converted into C++.

Here you need to find these packages (mine is under software\python3.7\Lib\site-packages) and copy (such as numpy, cv2 this folder) to the demo.distpath.

So far, the exe can run perfectly!

picture


Long press or scan the QR code below to get  free Python open courses and hundreds of gigabytes of learning materials packaged by the big guys , including but not limited to Python e-books, tutorials, project orders, source code, cracked software, etc.

picture

Scan the QR code - get it for free



Wonderful review of the past




PyCharm is really strong! ! !
Write a cool running every day in Python and
organize 18 Python crawler combat cases
8 Tips for Efficient Data Analysis in Python



Click  to read the original text to learn more