预编译pyc # py3.10.3 import compileallimport reimport timefrom pathlibimport Pathimport shutilfrom loguruimport loggerclass Pack ( object ) : '''打包''' def __init__ ( self, input : str , is_dir= True ) : self. input : Path= Path( input ) # 待编译的输入目录/单个文件 self. output: Path= self. get_desk_top( ) # 输出到桌面路径 self. is_dir: bool = is_dir# 是否为目录或单个文件 True为目录 False为单个文件 self. exclude_pattern= re. compile ( r"(\.venv/|\.git/)" ) # 排除的目录正则 def get_desk_top ( self) : '''获取桌面路径''' return Path. home( ) . joinpath( "Desktop" ) def pack_file ( self) : '''编译单个文件''' compileall. compile_file( fullname= str ( self. input ) , # 必选:单个文件的完整路径 force= True , # 强制重新编译 optimize= 2 , # 最高优化级别 quiet= 0 , legacy= True ) logger. debug( f'编译【 { self. input } 】成功!' ) _file_path, _file_name= self. input . parent, self. input . stem# 提取输入的文件路径和文件名(不含后缀) pyc_filename= f' { _file_name} .pyc' # 生成后的pyc文件名称 pyc_abs_path= Path( _file_path) . joinpath( pyc_filename) # 生成的pyc绝对路径 new_pyc_abs_path= self. output. joinpath( pyc_filename) # 要移动的目标绝对路径 shutil. move( pyc_abs_path, new_pyc_abs_path) # 移动 logger. success( f'移动文件【 { pyc_abs_path} 】-->【 { new_pyc_abs_path} 】' ) def pack_dir ( self) : '''编译整个项目''' compileall. compile_dir( dir = self. input , # 原项目目录 optimize= 2 , # 最高优化,提升反编译难度 maxlevels= 10 , # 递归深度无限制(0=不限制) quiet= 1 , # 0 表示完整输出,1 表示仅输出错误, legacy= True , # 如果为 True,则生成传统 pyc 路径,而不是 PEP 3147 路径 workers= 1 , # 最大并行工作线程数 force= True ) dir_name= self. input . name# 项目名 new_dir= self. output. joinpath( dir_name) # 复制到桌面的项目名 if new_dir. exists( ) : # 如果存在则删除 shutil. rmtree( new_dir) new_dir. mkdir( parents= True , exist_ok= True ) # 递归创建目录 for iin self. input . rglob( '*' ) : parts: tuple = i. partsif any ( ( '.git' in parts, 'venv' in parts, '.venv' in parts, '.idea' in parts) ) : # 排除文件 continue if i. is_dir( ) or i. suffix== '.py' : continue rel_path= i. relative_to( self. input ) # 得到当前文件和输入目录的相对路径 target_path= new_dir. joinpath( rel_path) # 得到目标输出绝对路径 target_path. parent. mkdir( parents= True , exist_ok= True ) # 创建目标输出的路径 if i. suffix== '.pyc' : # 如果是pyc文件移动 logger. success( f'移动:【 { i} 】-->【 { target_path} 】' ) shutil. move( i, target_path) else : # 负责为复制 logger. success( f'复制:【 { i} 】-->【 { target_path} 】' ) shutil. copy2( i, target_path) def start ( self) : '''开始''' if self. is_dir: logger. warning( f'编译(目录)文件:【 { self. input } 】' ) time. sleep( 0.5 ) self. pack_dir( ) else : logger. warning( f'编译(单独)文件:【 { self. input } 】' ) time. sleep( 0.5 ) self. pack_file( ) a= Pack( r"D:\jiexun\htsy" , is_dir= True ) a. start( ) 打包exe # coding=utf-8 py3.7.3 # @Time : 2025-09-01 02:04 # @Author : XiaoYi # @Email: 1206154726@qq.com import osimport shutilfrom loguruimport logger desktop_dist= os. path. join( os. path. join( os. path. expanduser( "~" ) , 'Desktop' ) , 'dist' ) desktop_main= os. path. join( desktop_dist, 'main' ) # from urllib.parse import quote as url_quote logger. debug( f'----------------------------开始构建程序--------------------------' ) os. system( 'pyinstaller main.py -i logo.ico' ) # 执行打包指令 pyinstaller -w main.py -i logo.ico 无调试窗口 logger. success( f'------->基础镜像结束,开始打包静态资源' ) shutil. move( os. path. join( os. getcwd( ) , 'dist' ) , desktop_dist) # 移动文件到桌面 logger. debug( f'------->【dist】基础镜像结束' ) shutil. rmtree( os. path. join( os. getcwd( ) , 'build' ) ) # 删除打包构建文件 os. remove( os. path. join( os. getcwd( ) , 'main.spec' ) ) # 删除打包构建文件 logger. warning( f'------->【编译文件清除】清除结束' ) shutil. copyfile( os. path. join( os. getcwd( ) , 'config.yaml' ) , os. path. join( desktop_main, 'config.yaml' ) ) shutil. copyfile( os. path. join( os. getcwd( ) , 'chromedriver.exe' ) , os. path. join( desktop_main, 'chromedriver.exe' ) ) os. rename( os. path. join( desktop_main, 'main.exe' ) , os. path. join( desktop_main, '新球体育.exe' ) ) # 重命名打包后的文件 os. rename( desktop_main, os. path. join( desktop_dist, 'xinqiutiyu' ) ) # 重命名打包后的文件 logger. success( f'-------【打包结束】----------' )