• 【python】py文件全自动打包成spec文件


    说明:

    自动获取当前根目录下所有py文件生成spec文件,直接运行pyinstaller进行打包即可。直接打包成单执行文件

    直接上代码

    import os
    
    pathex = []
    
    
    def recursion(path, main):
        if path[:1] != '/':
            path += '/'
        listpath = os.listdir(path)
        for item in listpath:
            if os.path.isdir(path + item):
                recursion(path + item, main)
                # print('文件夹',path +item)
            elif os.path.isfile(path + item):
                # if '.py' in item and '.pyc' not in item and '__init__' not in item and '.pyz' not in item and main not in item:
                if '.py' in item and all(i not in item for i in ['.pyc', '__init__', '.pyz','.pyd', main]):
                    # print('文件',path +item)
                    pathex.append(path + item)
            else:
                print('未知文件', path + item)
    
    
    
    def getTxt():
        return """# -*- mode: python ; coding: utf-8 -*-
    block_cipher = None
    
    
    a = Analysis(['${thisMain}.py'],
                 pathex=${thisPath},
                 binaries=[],
                 datas=[],
                 hiddenimports=${thisPy},             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='Main',
              debug=False,
              bootloader_ignore_signals=False,
              strip=False,
              upx=True,
              upx_exclude=[],
              runtime_tmpdir=None,
              console=True )
    
        """
    
    if __name__ == '__main__':
        # 要使用pyinstaller打包的项目路径
        # path = 'E:/pythonProject/test1/pyqt'
        path = os.path.dirname(__file__)
        # 打包项目的入口文件名
        main = 'main'
        recursion(path, main)
        hiddenimports = []
        for i in pathex:
            if i != path:
                # print(i[:i.rindex('/')])
                hiddenimports.append(i.replace(i[:i.rindex('/')], '').replace(i[i.index('.'):], '').replace('/', ''))
        pathexs = []
        pathex.append(path)
    
        for i in pathex:
            if i != path:
                pathexs.append(i.replace(os.path.abspath(os.path.join(os.getcwd(), "../")), '').replace('/', '\\'))
                continue
            pathexs.append(i.replace('/', '\\'))
    
        outTxt = getTxt().replace("${thisPy}",f'{hiddenimports}').replace("${thisPath}",f'{pathexs}').replace("${thisMain}",main)
        with open(f"{path}/{main}.spec","w",encoding="utf-8")as f:
            f.write(outTxt)
            f.close()
        exit(0)
    
    
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89

    打包后的spec文件怎么使用?

    直接在terminal窗口执行 pyinstaller 文件名.spec 然后回车即可

  • 相关阅读:
    Hibernate二级缓存
    JavaScript -- 06.函数知识汇总
    计算机视觉的优势和挑战
    JVM(十五)—— 垃圾回收(一)
    【mq】从零开始实现 mq-05-实现优雅停机
    Moonbeam上的多链用例解析——Derek在Polkadot Decoded 2022的演讲文字回顾
    洛谷P1763 埃及分数
    在家待了10多天,感觉自己真的不太适合居家
    VSCODE的常用插件
    AD使用技巧2
  • 原文地址:https://blog.csdn.net/qq_19883981/article/details/133975912