python 打包pyinstaller

pyinstaller

Pyinstaller可以通过简单的命令进行python代码的打包工作

spec文件结构

# -*- mode: python -*-

block_cipher = None


a = Analysis(['main.py'],
             pathex=['D:\\PythonProject\\mysoft'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
			 



#######!!!注意点1:加载自己的资源文件#####################
def extra_datas(mydir):
    def rec_glob(p, files):
        import os
        import glob
        for d in glob.glob(p):
            if os.path.isfile(d):
                files.append(d)
            rec_glob("%s/*" % d, files)
    files = []
    rec_glob("%s/*" % mydir, files)
    extra_datas = []
    for f in files:
        extra_datas.append((f, f, 'DATA'))

    return extra_datas

# append the 'Resources' dir
a.datas += extra_datas('Resources')	 ###这里是自己的资源文件夹	
a.datas += extra_datas('Reports')	 ###这里是自己的资源文件夹	
a.datas += extra_datas('Drivers')	 ###这里是自己的资源文件夹	
################################################
			 
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)

############打包成单个可执行文件###############
exe1 = EXE(pyz,
          a.scripts,
          a.binaries,                          ###!!!注意点2
          a.zipfiles,                          ###!!!注意点2
          a.datas,                             ###!!!注意点2
          [],
          exclude_binaries=False,   ###!!!注意点3:这里是False
          name='mysoft',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=False,
          icon='d:\\mysoft.ico')

#############打包免安装绿色文件夹################			 
exe2 = EXE(pyz,
          a.scripts,
          [],
          exclude_binaries=True,   ###!!!注意点3:这里是True
          name='mysoft',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=False,
          icon='d:\\mysoft.ico')


coll = COLLECT(exe2,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               name='mysoft')

实战

安装

pip3 install Pyinstaller

生成spec文件

pyi-makespec au2emotion.py

生成了au2emotion.spec

打包

pyinstaller -F au2emotion.spec -p D:\python_venv\au2emotion\Lib\site-packages

有些库依赖打包不进去,可以在主文件里import一下

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×