Python3.9 uses the latest version of pyinstaller to package projects or programs into executable files in exe or mac

1. Description of pyinstaller:

pyinstaller can package Python source files under Windows, Linux, Mac and other operating systems. By packaging the source files, the Python program can be run in an environment where Python is not installed, or it can be used as an independent file for easy delivery and management.

PyInstaller supports Python 2.7 and Python 3.3+. It can be used on Windows, Mac and Linux, but it is not cross-platform, but if you want to package it into an .exe file, you need to run PyInstaller on the Windows system for packaging; if you want to package it into a Mac App, you need to use it on Mac OS , Linux is the same, you can’t print the package of the other two ends on one end, and the package of Mac m1 can’t be used by Mac intel. On the contrary, it will report an error: Error: Bad CPU type in executable.

pyinstaller does not need to write the setup.py file by itself, it only needs to enter the packaging command in the working directory. Finally, build and dist folders will be generated, and the startup files are under the dist folder.

2. Installation of pyinstaller

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyinstaller

3. Packaging command

pyinstaller [项目启动文件]

Other parameters (choose according to requirements):

  • -F indicates that only a single executable file (including all dependencies inside) will be generated under the dist folder. If it is not added, a large number of dependent files + executable files will be generated in dist by default.
  • -D is opposite to -F
  • -w means to remove the console window, if your program has an interface, you don’t need to write this parameter. However, it is recommended to add this parameter first in the test case, because if the packaging is unsuccessful, the error message will be output on the console when running, and the error message cannot be seen without the console.
  • -c means to remove the window frame, use the console
  • -p means that you define the class path that needs to be loaded. When the project contains multiple self-built modules, you need to add -p aaa.py -p bbb.py -p ccc.py
  • -i indicates the icon for the executable, followed by the path to the icon
  • --hidden-import is followed by a module name such as queue, which is used to tell the packager that I don't need a certain module and you don't need to package it in

3.1, the command to package the project and code into multiple files

Under windows:

Project structure:

Order:

pyinstaller -D main.py -i ./sources/人工智能.ico -w

 Added files:

 The executable file of the target exe is in the dist folder:

 View the content in the main.spec generated above:

# -*- mode: python ; coding: utf-8 -*-


block_cipher = None


a = Analysis(
    ['main.py'],
    pathex=[],
    binaries=[],
    datas=[],
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(
    pyz,
    a.scripts,
    [],
    exclude_binaries=True,
    name='main',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    console=False,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
    icon=['sources\\人工智能.ico'],
)
coll = COLLECT(
    exe,
    a.binaries,
    a.zipfiles,
    a.datas,
    strip=False,
    upx=True,
    upx_exclude=[],
    name='main',
)

The following is the description for main.spec:

# -*- mode: python ; coding: utf-8 -*-


block_cipher = None


a = Analysis(
    ['main.py'], 
    pathex=[], # 此列表为项目绝对路径
    binaries=[],
    datas=[],  # 此处可以添加静态资源,格式为('SOURCE_DIR/TO/YOUR_FILES_PATH','TARGET_DIR_PATH/')
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(
    pyz,
    a.scripts,
    [],
    exclude_binaries=True,
    name='main', # 程序exe的名称
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True, # 打包的时候进行压缩,False表示不压缩
    console=False, #此处console=True表示,打包后的可执行文件双击运行时屏幕会出现一个cmd窗口,不影响原程序运行,如不需要执行窗口,改成False即可
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
    icon=['sources\\人工智能.ico'], #程序图标,要绝对路径,也可以不是相对路径
)
coll = COLLECT(
    exe,
    a.binaries,
    a.zipfiles,
    a.datas,
    strip=False,
    upx=True,
    upx_exclude=[],
    name='main', #程序文件夹名称
)

Change the main.spec file:

Note: Copy the source code to the dist directory, otherwise the program will not work! ! !

# -*- mode: python ; coding: utf-8 -*-


block_cipher = None


a = Analysis(
    ['main.py'],
    pathex=[], # 此列表为项目绝对路径
    binaries=[],
    datas=[('./src/','./src'),# 将源码输入进dist文件夹中,以保证程序正常运行
    ('./sources/','./sources/'), # 静态资源
    ('./source_dir/美女.png','./target_dir/')],# 此处可以添加静态资源,格式为('SOURCE_DIR/TO/YOUR_FILES_PATH','TARGET_DIR_PATH/')

    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(
    pyz,
    a.scripts,
    [],
    exclude_binaries=True,
    name='Speech演讲专用软件', # 程序exe的名称
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True, # 打包的时候进行压缩,False表示不压缩
    console=False, #此处console=True表示,打包后的可执行文件双击运行时屏幕会出现一个cmd窗口,不影响原程序运行,如不需要执行窗口,改成False即可
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
    icon=['sources\\人工智能.ico'], #程序图标,要绝对路径,也可以不是相对路径
)
coll = COLLECT(
    exe,
    a.binaries,
    a.zipfiles,
    a.datas,
    strip=False,
    upx=True,
    upx_exclude=[],
    name='WebBrowserSpeech', #程序文件夹名称
)

Then re-execute the following command:

pyinstaller main.spec

 The result becomes the following:

 

operation result:

 

You're done! ! !

Under Mac:

Project structure:

pyinstaller -D main.py -w -i ./sources/人工智能.icns

Added files:

 The executable on the target mac is under the dist folder:

 Change the main.spec file:

# -*- mode: python ; coding: utf-8 -*-


block_cipher = None


a = Analysis(
    ['main.py'],
    pathex=[],
    binaries=[],
    datas=[('./source_dir/美女.png','./target_dir/')],
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(
    pyz,
    a.scripts,
    [],
    exclude_binaries=True,
    name='Speech演讲专用软件',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    console=False,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
    icon=['sources/人工智能.icns'],
)
coll = COLLECT(
    exe,
    a.binaries,
    a.zipfiles,
    a.datas,
    strip=False,
    upx=True,
    upx_exclude=[],
    name='WebBrowserSpeech',
)
app = BUNDLE(
    coll,
    name='WebBrowserSpeech.app',
    icon='./sources/人工智能.icns',
    bundle_identifier=None,
)

Then execute the following command:

pyinstaller main.spec

 

Guess you like

Origin blog.csdn.net/wtl1992/article/details/131194417