记录实用有价值的内容

Python 使用 Pyinstaller 打包独立的 Mac 应用程序

使用 Pyinstaller 创建可独立运行的 Mac 桌面应用程序

简单使用:

pyinstaller --windowed --onefile --clean --noconfirm helloworld.py

执行以上命令后,将生成 ./dist/helloworld.app 应用程序,双击即可运行

同时,该命令生成了对应的打包配置文件 helloworld.spec,我们以后也可以通过此配置文件进行打包:

pyinstaller --windowed --onefile --clean --noconfirm helloworld.spec

通过配置文件,我们可以对打包程序做更多的自定义处理。

比如在Mac上打包的程序默认在 Retina 高清屏幕下是分辨率不够高的,呈模糊状态,可以添加配置 NSHighResolutionCapable 为 true 使显示清晰,还有其他配置如:版本号CFBundleVersion、版权信息NSHumanReadableCopyright 等

info_plist={
    'NSHighResolutionCapable': True,
    'NSHumanReadableCopyright': u'Copyright © 2019, InPanel, All Rights Reserved'
}

整个配置文件内容如下:

# -*- mode: python -*-

block_cipher = None

APP = ['helloworld.py']
APP_NAME = 'HelloWorld'

a = Analysis(APP,
             pathex=['/Users/douzhenjiang/Projects/python-gui'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             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,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name=APP_NAME,
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=False )
app = BUNDLE(exe,
            name='helloworld.app',
            icon=None,
            bundle_identifier=None,
            info_plist={
                'CFBundleName': APP_NAME,
                'CFBundleDisplayName': APP_NAME,
                'CFBundleGetInfoString': "Dou Zhenjiang",
                'CFBundleIdentifier': "com.uinote.helloworld",
                'CFBundleVersion': "0.0.1",
                'CFBundleShortVersionString': "0.0.1",
                'NSHighResolutionCapable': True,
                'NSHumanReadableCopyright': u"Copyright © 2019, UINOTE, All Rights Reserved"
            })


我们使用配置文件再次打包,此时的程序就可以在高清屏下显示分辨率正常了

pyinstaller --windowed --onefile --clean --noconfirm HelloWorld.spec

上一篇:python 获取文件名及后缀

下一篇:Python print() 显示颜色


唐钰豆豆

http://doudoudzj.com
尝试评论,很实用的技术,感谢分享!