python packages into executable files

Python is a scripting language that is interpreted and executed by an interpreter. How it is released:
  ● .py file: For open source projects or source code, it is not so important. To provide source code directly, users need to install Python and install various dependent libraries. (This is what Python's official installation packages do)
  ● .pyc file: Some companies or individuals do not want the source code to be seen by the operator due to confidentiality or various reasons, and can use the pyc file to publish, the pyc file is the Python interpreter Recognizable binary code, so it is also cross-platform after release, and users need to install the corresponding version of Python and dependent libraries.
  ● Executable files: For non-code farmers or some novice users, if you let him install Python and toss a bunch of dependent libraries, it is a disaster. For such users, the easiest way is to provide an executable file, just tell Ta the usage. What is more troublesome is that different executable files need to be packaged for different platforms (Windows, Linux, Mac, ...).
This article mainly introduces the last method. Both .py and .pyc are relatively simple, and Python itself can handle it. There are many ways to package a Python script into an executable file. This article focuses on PyInstaller, and the others are only for comparison and reference.
A comparison of Freezing Your Code
's various packaging tools is as follows (from the article Freezing Your Code):
Solution Windows Linux OS X Python 3 License One-file mode Zipfile import Eggs pkg_resources support
bbFreeze yes yes yes yes no MIT no yes yes yes
py2exe yes no no yes MIT yes yes no no
pyInstaller yes yes yes no GPL yes no yes no
cx_Freeze yes yes yes yes PSF no yes yes no
py2app no ​​no yes yes MIT no yes yes yes
PS. Among them, pyInstaller and cx_Freeze are both good, and some people on stackoverflow also suggested to use cx_Freeze, saying is more convenient. The new version of pyInstaller of pkg_resources seems to support it.
Installing PyInstaller
For those users who have a relatively stable network and can use the pip source address smoothly, the following command can be done directly:
pip install pyinstaller


Usually we will download the source package, then enter the package directory, and execute the following command (need to install setuptools): After
python setup.py install


is installed, check whether the installation is successful:
pyinstaller --version


After the installation is successful, you can use the following commands:
  ● pyinstaller : The main command for packaging executable files, the detailed usage will be introduced below.
  ● pyi-archive_viewer : View the list of files in the executable package.
  ● pyi-bindepend : View the dynamic libraries (.so or .dll files) that the executable depends on.
  ● pyi-... : and so on.
Using PyInstaller
The syntax of pyinstaller:
pyinstaller [options] script [script ...] |


The simplest usage of specfile, execute the command in the same directory as myscript.py:
pyinstaller mycript.py


Then you will see two new directories, build and dist , The files under dist are executable files that can be published. For the above command, you will find a bunch of files under the dist directory, all of which are dynamic library files and myscript executable files. Sometimes this is troublesome. You need to package everything under dist before publishing. If you lose a dynamic library, it will not work. Fortunately, pyInstaller supports single-file mode. You only need to execute:


pyinstaller -F mycript.py #with console
pyinstaller -w mycript.py #standalone executable app file
pyinstaller under Mac -w -F -i ico image full path mycript.py #window to generate a standalone exe file




You will find that there is only one executable file under dist, this single file can Released to run under a system similar to the operating system you are using.
Of course, pyinstaller also has various options, including general options, such as the -d option for debugging, to understand the process executed by pyInstaller; there are also some options for different platforms. For specific usage, you can visit the official WIKI of PyInstaller.
When executing the pyInstaller command, a .spec file will be generated in the same directory as the script, which will tell pyinstaller how to process all your scripts and include command options. Generally, we don't need to pay attention to this file. If you need to package data files, or add some Python runtime options to the packaged binary... Some advanced packaging options, you need to manually edit the .spec file. You can use:
pyi-makespec options script [script ...]


to create a .spec file. For manually edited .spec files, we can use any of the following commands:
pyinstaller specfile
pyi-build specfile


Introduction to the principle of
PyInstaller PyInstaller is actually a Packaging the python parser and your own script into an executable file is completely different from compiling into real machine code, so don't expect packaging into an executable file to improve the running efficiency, on the contrary, it may reduce the running efficiency. , the advantage is that there is no need to install python and the libraries your script depends on on the runner's machine. Under the Linux operating system, it mainly uses the ldd and objdump commands in the binutil toolkit.
PyInstaller enters the script you specify, first analyzes other scripts that the script depends on, then finds, copies, collects all related scripts, including the Python parser, and puts these files in a directory, or packaged into a inside the executable.
You can directly publish the files in the entire output folder, or the resulting executable file. You just need to tell the user that your app is self-contained and can run directly without installing other packages or a certain version of Python.
It should be noted that the executable file packaged by PyInstaller can only be in the same environment as the packaging machine system. In other words, it is not portable, and if it needs to run on different systems, it must be packaged for that platform.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324741712&siteId=291194637