PyInstaller: package python source code into exe executable program

1. Introduction to PyInstaller

PyInstaller is a third-party library that can package Python source files under Windows, Linux, Mac OS X and other operating systems. By packaging the source files, the Python program can run in an environment where Python is not installed, or as an independent Documents are easy to transfer and manage.

PyInstaller supports Python 2.7 and Python 3.3+. It can be used on Windows, Mac OS X and Linux, but it is not cross-platform, but if you want to package into an .exe file, you need to run PyInstaller on the Windows system for packaging work; to package into a mac app, you need to run it on Mac Use on OS.

Install and update

pip/pip3 install pyinstaller
pip/pip3 install --upgrade pyinstalle

note:

  1. The PyInstaller library will automatically install the PyInstaller command into the Python interpreter directory, which is the same as the pip or pip3 command path, so it can be used directly.
  2. Running PyInstaller on Windows also requires PyWin32 or pypiwin32, and pypiwin32 will be installed automatically when you install PyInstaller.

Two, PyInstaller generates executable programs

The command syntax of the PyInstaller tool is as follows:

pyinstaller 选项 Python 源文件

Regardless of whether the Python application is a single-file application or a multi-file application, just compile the Python program as the program entry point when using the pyinstaller command.

The PyInstaller tool is cross-platform. It can be used on both Windows and Mac OS X platforms. The method of using the PyInstaller tool on different platforms is the same, and the options they support are also the same.

Let's first create an app directory, and create an app.py file in this directory. The file contains the following code:

def main():
	print('程序开始执行')

if __name__ == '__main__':
	main()

Next, use the command line tool to enter the app directory and execute the following command:

pyinstaller -F app.py

Execute the above command, you will see the detailed generation process. When the generation is complete, you will see an additional dist directory in the app directory, and an app.exe file in this directory. This is the EXE program generated by the PyInstaller tool.

Enter the dist directory in the command line window, execute app.exe in the directory,

dist> ./app.exe

You will see that the program produces the following output:

程序开始执行

Since the program does not have a graphical user interface, if readers try to run the program by double-clicking, they can only see that the program window disappears in a flash, so that the output of the program cannot be seen.

The -F option is used in the above command. This option specifies that a separate EXE file is generated. Therefore, a separate app.exe file of approximately 6MB is generated in the dist directory (the file generated on the Mac OS X platform is called app, without suffix); Corresponding to the -F option is the -D option (default option), which specifies to generate a directory (containing multiple files) as a program.

First delete the build and dist directories generated by the PyInstaller tool in the app directory, and delete the app.spec file, and then use the following command to generate the EXE file.

pyinstaller -D app.py

Execute the above command, you will see the detailed generation process. When the generation is complete, you will see an additional dist directory in the app directory, and an app subdirectory in this directory, which contains a large number of .dll files and .pyz files, all of which are It is the supporting file of the app.exe program. Run the app.exe program in the command line window, and you can also see the same output result as the previous app.exe program.

PyInstaller not only supports -F and -D options, but also supports common options as shown in Table 1.

-h,–help View the help information of the module
-F,-onefile Generate a single executable file
-D , –onedir Generate a directory (containing multiple files) as an executable program
-a,–ascii Does not include Unicode character set support
-d,–debug Generate a debug version of the executable file
-w,–windowed,–noconsolc Specifies not to display the command line window when the program is running (only valid for Windows)
-c,–nowindowed,–console Specify to use the command line window to run the program (only valid for Windows)
-o DIR , –out = DIR Specify the generation directory of the spec file. If not specified, the current directory is used to generate the spec file by default
-p DIR , –path = DIR Set the path of the Python import module (similar to setting the PYTHONPATH environment variable). You can also use path separators (a semicolon for Windows and a colon for Linux) to separate multiple paths
-n NAME,–name=NAME Specify the name of the project (the generated spec). If this option is omitted, the main file name of the first script will be used as the name of the spec

Guess you like

Origin blog.csdn.net/u013250861/article/details/114297021