Python

python3打包

  1. 首先安装pyinstaller

个人认为试过几个,这个最好用,在window、linux、mac都可以使用

pip install pyinstaller

  1. 单个文件打包

    直接使用pyinstaller xxx.py

    打包结束后在当前文件夹生成dist文件夹和xxx.spec文件,dist文件夹中便是执行文件,xxx.spec文件稍后再说

  2. 多文件打包

    打开之前生成的xxx.spec文件可以看到一些打包信息,要打包多文件只需要对spec文件做出修改,添加相关文件和路径就可以,然后执行pyinstaller –D ./xxx.spec即可

  3. 下面以我的为例,打包单个文件直接使用pyinstaller upload.py,然后生成upload.spec文件如下:

    # -*- mode: python -*-

     

    block_cipher = None

     

     

    a = Analysis([‘upload.py’],

    pathex=[‘D:\\software\\python\\py_upload\\upload\\upload’],

    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,

    [],

    exclude_binaries=True,

    name=’upload’,

    debug=False,

    bootloader_ignore_signals=False,

    strip=False,

    upx=True,

    console=True )

    coll = COLLECT(exe,

    a.binaries,

    a.zipfiles,

    a.datas,

    strip=False,

    upx=True,

    name=’upload’)

     

     

    添加相关文件和路径如下,可以使用相对路径,然后执行pyinstaller –D ./upload.spec

     

    # -*- mode: python -*-

     

    block_cipher = None

     

     

    a = Analysis([‘upload.py’,

                    ‘uploadM.py’,

                    ‘./ui/ui_firmware.py’,

                    ‘./ui/ui_login.py’,

                    ‘./ui/ui_project.py’,

                    ‘./ui/ui_upload.py’,

                    ‘./ui/ui_user.py’,

                    ‘./login/loginC.py’,

                    ‘./login/loginM.py’,

                    ‘./function/userM.py’,

                    ‘./function/userC.py’,

                    ‘./function/projectM.py’,

                    ‘./function/projectC.py’,

                    ‘./function/firmwareM.py’,

                    ‘./function/firmwareC.py’],

    pathex=[‘./’,

                 ‘./ui’,

                 ‘./login’,

                 ‘./function’],

    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,

    [],

    exclude_binaries=True,

    name=’upload’,

    debug=False,

    bootloader_ignore_signals=False,

    strip=False,

    upx=True,

    console=True )

    coll = COLLECT(exe,

    a.binaries,

    a.zipfiles,

    a.datas,

    strip=False,

    upx=True,

    name=’upload’)

     

    执行pyinstaller –D ./upload.spec后可能需要输入y进行确认覆盖

     

     

5、其他帮助可查看help

 

 

闻道有先后
术业有专攻

From: shaynerain.com

By: Shaynerain

1 COMMENTS

LEAVE A RESPONSE

电子邮件地址不会被公开。

Hi, I'm shaynerain, this is my website. www.shaynerain.com