Analysis of the advantages and disadvantages of six methods of packaging Python code

Python is a high-level programming language that is easy to learn, easy to use, and cross-platform, so it has been widely used in development. However, Python code needs to be run in the Python interpreter, which may be inconvenient for some users.

As an interpreted language, Python makes the source code public upon release. Although it advocates open source, sometimes you just can’t help but want to package it into an exe, not only to encrypt the code, but also to cross-platform. This prevents some computers that do not have the py environment installed from being unable to run the software.

Therefore, packaging Python code into an executable file (exe) is a good solution. This article will analyze the advantages and disadvantages of several packaging methods.

Use pyinstaller

pyinstaller is a popular Python packaging tool that can package Python code into independent executable files.

Using pyinstaller to package Python code is very simple, just enter the following command on the command line: pyinstaller your_script.py

This will generate an executable file that can run on Windows, Linux and MacOS.

Use cx_Freeze

cx_Freeze is another popular Python packaging tool that can package Python code into a standalone executable file.

It is also very simple to use cx_Freeze to package Python code. You only need to enter the following command on the command line: cxfreeze your_script.py –target-dir dist

This will generate an executable file that can run on Windows, Linux and MacOS.

Use py2exe

py2exe is a Python packaging tool that can package Python code into a Windows executable file.

It is also very simple to use py2exe to package Python code. You only need to enter the following command on the command line: python setup.py py2exe

This will generate a Windows executable file.

Use py2app

py2app is a Python packaging tool that can package Python code into MacOS executable files.

It is also very simple to package Python code using py2app. You only need to enter the following command on the command line: python setup.py py2app

This will generate a MacOS executable file.

Using Nuitka

Nuitka is a Python compiler that compiles Python code into C++ code and then packages it into an executable file.

It is also very simple to package Python code with Nuitka. You only need to enter the following command on the command line: nuitka your_script.py

This will generate an executable file that can run on Windows, Linux and MacOS.

Using Nuitka + pyinstaller

Nuitka and pyinstaller can be used together to compile Python code into C++ code, which is then packaged into an executable file using pyinstaller.

Packaging Python code using Nuitka + pyinstaller is also very simple, just enter the following command on the command line:

nuitka –standalone your_script.py

pyinstaller your_script.spec

This will generate an executable file that can run on Windows, Linux and MacOS.

Summary

This article introduces 6 ways to package Python code into exe applications, including pyinstaller, cx_Freeze, py2exe, py2app, Nuitka and Nuitka + pyinstaller. Each method has its advantages and disadvantages. For example, pyinstaller is what I use most.

Advantages: python files can be converted into executable files; cross-platform; the output can be a single directory or a separate packaged executable file; py2exe looks like an output directory; intelligent third-party modules that support python such as PyQt , external data files, etc.; supports EGG format files; executable files can be compressed using UPX and binary compression; supports console and window modes; can select the icon of the exe file (Windows only); supports COM server (Windows only) .

Disadvantages: Problems with import: pyinstaller is very smart. As long as the entry py file is specified, it will automatically find the package that needs to be imported based on the code. But if it is imported implicitly, there will be no problem in normal operation. example:

**# test1.py**

**from sqlalchemy import create_engine**

**from sqlalchemy.orm import sessionmaker**

**DB_CONNECT_STRING = 'mysql + pymysql://root:123456@localhost/study'**

**engine = create_engine(DB_CONNECT_STRING, echo = False)**

**DB_Session = sessionmaker(bind = engine)**

**session = DB_Session()**

**print('this is my test')**

**There is no problem running the initialization engine of this ORM library. The result is obtained in the console: this is my test**

**Then we start packaging, using the simplest pyinstaller test1.py. After packaging is completed, there is a dist folder in the current directory. Enter the test1 folder under dist, then open cmd and run this exe. We will find: **

**Tip: no model named 'pymysql'**

What’s going on? Then the question arises. The sqlalchemy library does not need to display the import engine library when it is initialized. It has its own create_engine() function for initialization. This string is filled in by the user according to the rules. In fact, the solution is very simple, we only need to explicitly import the pymysql library. Now we import this library: import pymysql

Repackage it (remember to delete the spec file when repackaging, otherwise there will be cache, or add the –clean option to clear it), run it again, and now there is no such problem.

Problems with multi-process packaging

There is a GIL lock in the official CPython. The existence of this lock has many advantages. Many libraries are thread-safe and single-threaded execution is also efficient. In an early version of python, the GIL was canceled and replaced with high-granularity locks to implement multi-threading. However, the efficiency of a single thread in actual applications was greatly reduced. Therefore, the GIL lock was restored later, so whether it is python2 or python3, this lock will be included. But there is a big problem with this lock, and that is the efficiency issue, which causes Python to only use one core to calculate data. So later, in order to make up for the problems caused by this GIL, experts designed the multiprocessing library, gevent library, etc.

The former is a multi-process library, in order to solve the situation where Python is used for data-intensive processing; the latter is used for asynchronous IO processing. The basic principle is to switch back and forth between CPU clocks. A simple example is a crawler program crawling a web page. when. If there are 10 URLs, we have to GET them. In fact, the delay between the networks is much higher than within the computer, so the computer will switch to the next one during this time. Sometimes it is necessary to use multiple processes. This cannot be replaced, even if it takes up a lot of resources.

In short, developers can choose the appropriate method according to their own needs. No matter which method you choose, Python code can be packaged into a stand-alone executable file for user convenience.

Finally:

I have prepared some very systematic Python materials. In addition to providing you with a clear and painless learning path, I have also selected the most practical learning resources and a huge library of mainstream crawler cases< /strong>. After a short period of study, you will be able to master the crawler skill well and obtain the data you want. Friends who need it can scan the QR code at the end of the article to obtain it.

01 is specially designed for 0 basic settings, and even beginners can learn it easily

We have interspersed all the knowledge points of Python into the comics.

In the Python mini-class, you can learn knowledge points through comics, and difficult professional knowledge becomes interesting and easy to understand in an instant.

Just like the protagonist of the comic, you travel through the plot, pass the levels, and complete the learning of knowledge unknowingly.

02 No need to download the installation package yourself, detailed installation tutorials are provided

03 Plan detailed learning routes and provide learning videos

04 Provide practical information to better consolidate knowledge

05 Provide interview information and side job information to facilitate better employment


This complete version of Python learning materials has been uploaded to CSDN. If you need it, you can scan the official QR code of csdn below or click on the WeChat card at the bottom of the homepage and article to get the method. [Guaranteed 100% free]

syntaxbug.com © 2021 All Rights Reserved.