This article brings you how to package a Python script into an executable file? (Details), it has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Python is a scripting language that is interpreted and executed by the interpreter. Its release method:
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 Python scripts into executable files. This article focuses on PyInstaller, and the others are only for comparison and reference.
The comparison of 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 | 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. Some people on stackoverflow also suggested using cx_Freeze, saying that it is more convenient. The new version of pkg_resources seems to be supported by pyInstaller.
Installing PyInstaller
For those users whose network is relatively stable and can use the pip source address smoothly, just run the following command:
pip install pyinstaller
Usually we will download the source code package, then enter the package directory and execute the following command (setuptools needs to be installed):
python setup.py install
After installation , check whether the installation is successful:
pyinstaller --version
After the installation is successful, you can use the following command:
pyinstaller
: The main command for packaging executable files. Detailed usage will be introduced below.
pyi-archive_viewer
: View the file list in the executable package.
pyi-bindepend
: View the dynamic libraries (.so or .dll files) that the executable file depends on
pyi-...
: Wait.
Use PyInstaller
pyinstaller
's syntax:
pyinstaller [options] script [script ...] | specfile
Most Simple usage, execute the command in the same directory as myscript.py:
pyinstaller mycript.py
Then you will see that two new directories, build and dist, have been added, below dist The file is an executable file that can be published. For the above command, you will find a bunch of files under the dist directory, including dynamic library files and myscrip executable files. Sometimes this feels troublesome. You need to package everything under dist before publishing. If you lose a dynamic library, it will not run. Fortunately, pyInstaller supports single file mode. You only need to execute:
pyinstaller -F mycript.py
You will find that there is only one executable file under dist. This single file can be published and can 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 and understanding the execution process of pyInstaller; there are also some options for different platforms, specific usage You can visit the PyInstaller official WIKI.
When executing the pyInstaller
command, a .spec
file will be generated in the same directory as the script. This file will tell pyinstaller how to process all your scripts. , which also contains command options. Generally we don't need to pay attention to this file. If we need to package data files, or add some Python runtime options to the packaged binary... some advanced packaging options, we 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 actually packages the python parser and your own script into an executable file and compiles it into real machine code They are completely different things, so don't expect that packaging into an executable file will improve operating efficiency. On the contrary, it may reduce operating 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
tool package.
PyInstaller enters the script you specified, first analyzes other scripts that the script depends on, then searches, copies, collects all related scripts, including the Python parser, and then puts these files in a directory Download it, or package it into an executable file.
You can directly publish the files in the entire output folder, or the generated executable file. You only need to tell users that your application is self-contained and does not require the installation of other packages or a certain version of Python before it can be run directly.
It should be noted that the executable files packaged by PyInstaller can only be used in the same environment as the packaging machine system. In other words, it is not portable and must be packaged for that platform if it needs to run on a different system.
The above is the detailed content of How to package a Python script into an executable file? (detailed). For more information, please follow other related articles on the PHP Chinese website!