Package Python scripts into executable files

Source: Internet
Author: User

Package Python scripts into executable files

Python is a scripting language that is interpreted and executed by the interpreter. Its publishing method:

. Py file: for open-source projects or source code that is not so important, directly provide the source code, you need to install Python and install various libraries on your own. (This Is What Python official installation packages do)

. Pyc file: Some companies or individuals are reluctant to view the source code because of confidentiality or various reasons. You can use the pyc file to publish the file. The pyc file is a binary code that can be recognized by the Python interpreter, therefore, it is also cross-platform after release. You need to install Python and dependent libraries of the corresponding version.

Executable files: for non-code farmers or some small-lived users, it is a disaster to have them install Python and have a bunch of dependent libraries at the same time. For such users, the simplest way is to provide an executable file. You only need to tell him the usage. It is troublesome to package different executable files (Windows, Linux, Mac,…) for different platforms ,...).

This article mainly introduces the last method. Both. py and. pyc are simple, and Python can be done by itself. There are multiple methods to package Python scripts into executable files. This article focuses on PyInstaller. Others are for comparison and reference only.

Freezing Your Code

The comparison of various packaging tools is as follows (from 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, And cx_Freeze is also recommended for stackoverflow, which is more convenient. Pkg_resources the new version of pyInstaller seems to be supported.

Install PyInstaller

For those users whose networks are stable and can smoothly use the pip source address, you can directly run the following command:

pip install pyinstaller

We usually download the source code package, 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: Main commands for packaging executable files. detailed usage is described below. pyi-archive_viewer: View the file list in the executable package. pyi-bindepend: View the dynamic library (. so or. dll file) on which the executable file depends) pyi-...: And so on. Use PyInstaller

pyinstallerSyntax:

pyinstaller [options] script [script ...] | specfile

In the simplest usage, execute the command in the same directory as myscript. py:

pyinstaller mycript.py

Then we will see two newly added directories: build and dist. The files under dist are executable files that can be released. For the above commands, you will find a bunch of files under the dist directory, various dynamic library files and myscrip executable files. Sometimes this is troublesome. You need to package all the things under dist to publish it. If you lose a dynamic library, you cannot run it. Fortunately, pyInstaller supports the single file mode and only needs to execute:

pyinstaller -F mycript.py

You will find that there is only one executable file under dist, and this single file can be published and run under a system similar to the operating system you are using.

Of course,pyinstallerThere are also a variety of options, there are general options, such as the-d option for debug, understand the pyInstaller execution process; there are some options for different platforms, specific usage can visit the PyInstaller official WIKI.

In executionpyInstallerIn the same directory as the script,.specFile, which tells pyinstaller how to process all your scripts and contains Command Options. In general, we don't need to ignore this file. If we need to package the data file or add some Python runtime options to the packaged binary file... You must manually edit some advanced packaging options..specFile. You can use:

pyi-makespec options script [script ...]

Create a. spec file. You can use any of the following commands to manually edit the. spec file:

pyinstaller specfilepyi-build specfile
Principles of PyInstaller

PyInstaller is actually packaging the python parser and your own script into an executable file, which is totally different from the compiled real machine code, therefore, it is not expected that packaging into an executable file will improve the running efficiency. On the contrary, it may reduce the running efficiency, the advantage is that python and the library on which your script depends are not installed on the machine of the runner. In Linux, it mainly usesbinutilIn the toolkitlddAndobjdumpCommand.

PyInstaller input the script you specified, first analyze other scripts on which the script depends, then search, copy, and collect all related scripts, including the Python parser, put these files in a directory or package them into an executable file.

You can directly publish the output files in the entire folder or generate executable files. You only 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.

Note that the execution file packaged by PyInstaller can only be in the same environment as the packaging machine system. That is to say, it is not portable. To run on different systems, you must package the platform.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.