Home > Backend Development > Python Tutorial > Python's packaging artifact—Nuitka!

Python's packaging artifact—Nuitka!

WBOY
Release: 2023-04-12 23:31:12
forward
2119 people have browsed it

Python's packaging artifact—Nuitka!

1. Experience using pyinstaller and Nuitka

1.1 Usage requirements

This time it is also because The project needs to convert 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 obtained here prevents decompilation), and then compiles it into an executable file.
  • Convenient for transplantation. It is convenient for users to use, and there is no need to install python, third-party packages or the like.

1.2 Feelings of using

The biggest feeling after using these two tools is:

  • The pyinstaller experience is very poor!
  • The exe converted into a deep learning project is nearly 3 G in size (pyinstaller is to install the entire running environment Packaging), yes, you heard it right, an EXE has 3 G!
  • Packaging is super slow and startup is super slow.
  • nuitka smells so good!
  • For the same project, the generated exe is only 7M!

  • #Packaging is super fast (within 1 minute) and startup is super fast.

##2. Installation and use of Nuitka

2.1 Installation of nuitka

  • You can install it directly using pip :pip install Nuitka<span style="font-size: 15px;"></span>
  • Download vs2019 (MSVS) or MinGW64, they are all C compilers anyway, just download them.
2.2 Usage process

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

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文件
Copy after login

Use the following command (debugging) 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
Copy after login

Here is a brief introduction to my nuitka command above:

  • <span style="font-size: 15px;">--standalone</span>:方便移植到其他机器,不用再安装python
  • <span style="font-size: 15px;">--show-memory --show-progress</span>:展示整个安装的进度过程
  • <span style="font-size: 15px;">--nofollow-imports</span>:不编译代码中所有的import,比如keras,numpy之类的。
  • <span style="font-size: 15px;">--plugin-enable=qt-plugins</span>:我这里用到pyqt5来做界面的,这里nuitka有其对应的插件。
  • <span style="font-size: 15px;">--follow-import-to=utils,src</span>:需要编译成C++代码的指定的2个包含源码的文件夹,这里用<span style="font-size: 15px;">,</span>来进行分隔。
  • <span style="font-size: 15px;">--output-dir=out</span>:指定输出的结果路径为out。
  • <span style="font-size: 15px;">--windows-icon-from-ico=./logo.ico</span>:指定生成的exe的图标为logo.ico这个图标,这里推荐一个将图片转成ico格式文件的网站(比特虫)。
  • <span style="font-size: 15px;">--windows-disable-console</span>:运行exe取消弹框。这里没有放上去是因为我们还需要调试,可能哪里还有问题之类的。

经过1min的编译之后,你就能在你的目录下看到:

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

当然这里你会发现真正运行exe的时候,会报错:​<span style="font-size: 15px;">no module named torch,cv2,tensorflow</span>​等等这些没有转成C++的第三方包。

这里需要找到这些包(我的是在softwarepython3.7Libsite-packages下)复制(比如numpy,cv2这个文件夹)到​<span style="font-size: 15px;">demo.dist</span>​路径下。

至此,exe能完美运行啦!

The above is the detailed content of Python's packaging artifact—Nuitka!. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template