How to package a Python script into an executable file? (detailed)

不言
Release: 2018-09-27 15:30:51
Original
5919 people have browsed it

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:

  • .py file: For open source projects or where the source code is not that important, the source code is provided directly, and the user needs to install Python and various dependent libraries by themselves. (This is what Python 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. You can Publish using pyc files. The pyc file is a binary code that can be recognized by the Python interpreter, so it is cross-platform after publishing. Users need to install the corresponding version of Python and dependent libraries.

  • Executable file: For non-coders or novice users, it would be a disaster if you ask them to install Python and also have to deal with a bunch of dependent libraries. For such users, the easiest way is to provide an executable file and just tell them how to use it. 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 Python scripts into executable files. This article focuses on PyInstaller, and the others are only for comparison and reference.

Freezing Your Code

The comparison of various packaging tools is as follows (from the article Freezing Your Code):

##bbFreezeyesyesyesnoMITnoyesyesyespy2exeyesnonoyesMITyesyesnonopyInstalleryesyes yesnoGPLyesnoyesnocx_FreezeyesyesyesyesPSFnoyesyesnopy2appnonoyesyesMITnoyesyes 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
Copy after login

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

After installation , check whether the installation is successful:

pyinstaller --version
Copy after login

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

Most Simple usage, execute the command in the same directory as myscript.py:

pyinstaller mycript.py
Copy after login

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

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 ...]
Copy after login

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

pyinstaller specfile
pyi-build specfile
Copy after login

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.

Solution Windows Linux OS X Python 3 License One-file mode Zipfile import Eggs pkg_resources support

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!

Related labels:
source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!