To publish a Python program as an executable file

Source: Internet
Author: User

It is no problem to run your own Python program in your own Python environment. But if you want to publish it to someone else, it needs to have a python environment. Otherwise, we will need to publish our Python program as an executable file.

Pyinstaller

Pyinstaller is a useful third-party library that can package Python source files in Windows, Linux, Mac OS X and other operating systems, and Python programs can run in environments where Python is not installed by packaging the source files. can also be used as a standalone file for easy delivery and management.
Pyinstaller's official website: http://www.pyinstaller.org/

Installation

You can use the PIP command to complete the installation:

pip install pyinstaller

But maybe the source of the foreign is the wall, here can also specify a third-party source to install:

pip install -i https://mirrors.163.com/pypi/simple pyinstaller

After the installation is complete, a Pyinstaller.exe file will be generated in your Scripts folder, which is the Pip.exe folder. Then use this to package our program. There is also a file that is Pyi-makespec.exe.

Direct Packaging Program

Use the following command to package the program:

pyinstaller <主程序名>.py

Build and Dist Two folders will be generated after the completion of the finished call. Where the build directory is the directory where Pyinstaller stores temporary files and can be safely removed. The final packager is in the Dist internal < main program name > directory. There is also a < main program name >.spec file, which will be discussed below, but also can be deleted.
Note: Your program cannot contain other resource files in such a package. No import files should not be packaged in, it seems that the program itself has no way to determine what resources to include, always can not be a sentence to analyze your source bar.
In fact, the manual copy of your resource file in the past is OK, but this is not the normal path.

Generate Spec file and repackage

By editing a specification file, tell the program which resource files to include, and then package it with this specification file. command to generate the default spec file:

pyi-makespec <主程序名>.py

In fact, before the direct packaging, is also the default specification file, and then in accordance with this specification file packaging. This just divides the process into 2 steps. In the middle you can modify the specification file and then go to the next package.
The default file contents are as follows:

# -*- mode: python -*-block_cipher = Nonea = Analysis([‘showlist.py‘],             pathex=[‘I:\\Python自动化开发学习\\作业\\week1\\多级菜单‘],             binaries=[],             datas=[],             hiddenimports=[],             hookspath=[],             runtime_hooks=[],             excludes=[],             win_no_prefer_redirects=False,             win_private_assemblies=False,             cipher=block_cipher)pyz = PYZ(a.pure, a.zipped_data,             cipher=block_cipher)exe = EXE(pyz,          a.scripts,          exclude_binaries=True,          name=‘showlist‘,          debug=False,          strip=False,          upx=True,          console=True )coll = COLLECT(exe,               a.binaries,               a.zipfiles,               a.datas,               strip=False,               upx=True,               name=‘showlist‘)

Modify the datas variable and add your resource file to the list later. In the following example, I first define a variable added_files and then assign the list to datas. The list element is a tuple, preceded by the path to your resource (here is the relative path), followed by where to place the packaged file, as follows:

# -*- mode: python -*-block_cipher = Noneadded_files = [    (‘poetry‘, ‘poetry‘),    (‘list.txt‘, ‘.‘),]a = Analysis([‘showlist.py‘],             pathex=[‘I:\\Python自动化开发学习\\作业\\week1\\多级菜单‘],             binaries=[],             datas=added_files,             hiddenimports=[],             hookspath=[],             runtime_hooks=[],             excludes=[],             win_no_prefer_redirects=False,             win_private_assemblies=False,             cipher=block_cipher)pyz = PYZ(a.pure, a.zipped_data,             cipher=block_cipher)exe = EXE(pyz,          a.scripts,          exclude_binaries=True,          name=‘showlist‘,          debug=False,          strip=False,          upx=True,          console=True )coll = COLLECT(exe,               a.binaries,               a.zipfiles,               a.datas,               strip=False,               upx=True,               name=‘showlist‘)

After the specification file has been modified by specification file packaging , use the following command to specify the specification file for packaging:

pyinstaller <规范文件名>.spec

Once the execution is complete, the packaged program now contains the resource files we have added. actually the effect is the same as when we manually copied it ourselves.
Modify the specification file, you do not need to use the following parameters.

Useful parameters

You can see the description of the parameter using-H, and here are a few more useful parameters.
In addition, the use of parameters is not required to use the specification file. Using the nature of the parameter or a specification file, the specification file will be changed according to our parameters, and then packaged by a canonical file. You can verify the comparison by viewing the automatically generated specification file after you have packaged the parameters.

Generate a single file (-F,--onefile)

Command:

pyinstaller -F <主程序名>.py或pyinstaller <主程序名>.py -F

I'm trying to come up here. Directly package a program that does not contain resource files. After the success of the Dist folder there will be only one executable file, and the size is much smaller than the previous folder. But the online said the startup program will be slow, own grasp.

If the specification file that contains the resource file is packaged with the-f parameter, the last one is a directory.

Specify a resource file (--add-data "src;dest or Src:dest")

The front is the source, followed by the end, and the middle is the delimiter. The Windows system uses a semicolon (;), and the Linux system uses a colon (:). And this parameter can be used multiple times. Here are 2 additional resource files:

pyinstaller showlist.py --add-data poetry;poetry --add-data list.txt;.

The final effect is still the same as the above using the specification file, the feeling is still a good way to standardize the file. Here you can compare the system automatically generated specification files.
Here we try to use with-F, but the generated single file does not contain a resource file. It is probably not possible to package a resource file together into a single file. However, it is still possible to copy the resource files in the past. That is, your main program or a single file, but the resource files are independent outside.

Specify icon (-i "File.ico or File.exe,id or File.icns",--icon "File.ico or File.exe,id or File.icns")

It's pretty useful to specify an icon, but if you try, the single file is still seemingly invalid. Only use folders, and then the icon inside the main program becomes the icon you specify.

pyinstaller <主程序名>.py -i <图标>.ico

Similarly, the specification file also has a place to specify the icon:

exe = EXE(pyz,          a.scripts,          exclude_binaries=True,          name=‘showlist‘,          debug=False,          strip=False,          upx=True,          console=True , icon=‘name.ico‘)
Summary
    • Recommend Mr. Cheng as a spec file, then edit it and package it in a canonical file
    • Or do not create a single file, I try to be more chicken
    • Use or communicate with others do not have to pack, dozens of K code package down more than 10 trillion. Because there's no python environment, it's supposed to be a library that's going to be used.
    • The final package released to the layman use or good, in an icon packaging, nice.
Expand the-WX module

There is a WX module that is used to make the interface.
Make a window interface, wrap up our Python program, and finally package it as an executable to publish. Such a python software comes out.

To publish a Python program as an executable file

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.