吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1530|回复: 15
收起左侧

[Python 原创] 【python】py文件全自动打包成spec文件

[复制链接]
黑猫的猫 发表于 2023-10-22 18:21
本帖最后由 黑猫的猫 于 2023-10-23 01:18 编辑

说明:

自动当前根目录下所有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)

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

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

免费评分

参与人数 2吾爱币 +6 热心值 +2 收起 理由
苏紫方璇 + 5 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
hrh123 + 1 + 1 用心讨论,共获提升!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

sai609 发表于 2023-10-22 18:47
进去文件路径,双击文件,运行
darksetyaer 发表于 2023-10-22 19:15
li645944229 发表于 2023-10-22 20:01
fkmol 发表于 2023-10-22 20:10
打包exe会不会更方便点?
mcby 发表于 2023-10-22 20:40
感谢 分享
hbzy 发表于 2023-10-22 21:08
虽然看不懂但还是看了,因为只有看了才知道不懂
txj66 发表于 2023-10-22 21:22
不错,学习了,有机会试试
建国搬运工 发表于 2023-10-22 21:41
学习一下
HuTaojhl 发表于 2023-10-22 22:18
学习一下
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-15 07:42

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表