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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
| import sys import os import glob import site import importlib import pkgutil from PyInstaller.utils.hooks import collect_data_files, collect_submodules, collect_all
block_cipher = None
def collect_submodules_recursive(package_name, exclude_patterns=None, max_depth=10): """ 递归收集包的所有子模块(包括多层嵌套) Args: package_name: 包名,如 'app' exclude_patterns: 排除的模式列表,如 ['*.tests', '*.sanic'] max_depth: 最大递归深度 Returns: list: 所有子模块名称列表 """ import fnmatch modules = [] seen = set() if exclude_patterns is None: exclude_patterns = ['*.tests', '*.test', '*.examples'] def should_exclude(module_name): """检查模块是否应该被排除""" for pattern in exclude_patterns: if fnmatch.fnmatch(module_name, pattern): return True return False def _collect(pkg_name, pkg_path, depth=0): """递归收集子模块""" if depth > max_depth: print(f"达到最大深度 {max_depth},停止递归 {pkg_name}") return indent = " " * depth if pkg_name not in seen and not should_exclude(pkg_name): seen.add(pkg_name) modules.append(pkg_name) print(f"{indent} 收集包: {pkg_name}") try: for module_info in pkgutil.iter_modules([pkg_path]): module_name = f"{pkg_name}.{module_info.name}" if should_exclude(module_name): print(f"{indent} 跳过: {module_name}") continue if module_name not in seen: seen.add(module_name) modules.append(module_name) print(f"{indent} 收集模块: {module_name}") if module_info.ispkg: try: sub_module = importlib.import_module(module_name) if hasattr(sub_module, '__path__'): sub_path = sub_module.__path__[0] _collect(module_name, sub_path, depth + 1) except Exception as e: print(f"{indent} 无法导入子包 {module_name}: {e}") except Exception as e: print(f"{indent} 迭代 {pkg_name} 时出错: {e}") try: pkg = importlib.import_module(package_name) if hasattr(pkg, '__path__'): package_path = pkg.__path__[0] print(f"\n开始递归收集 {package_name} (路径: {package_path})") _collect(package_name, package_path) print(f"收集完成,共 {len(modules)} 个模块\n") else: print(f"{package_name} 不是包") return [] except Exception as e: print(f"无法导入 {package_name}: {e}") return [] return modules
pyDOE3_datas = collect_data_files('pyDOE3')
app_modules = collect_submodules_recursive( 'app', exclude_patterns=['*.tests', '*.test', '*.pyc'] )
a = Analysis( ['main.py'], pathex=[], binaries=[], datas=[ ('config.json', '.'), ('data', 'data'), ('dist', 'dist'), ('app/algorithm/libs', 'app/algorithm/libs') if os.path.exists('app/algorithm/libs') else None, ] + pyDOE3_datas, hiddenimports=[ 'uvicorn', 'uvicorn.logging', 'uvicorn.loops.auto', 'uvicorn.protocols.http', 'uvicorn.protocols.http.auto', 'fastapi', 'fastapi.routing', ] + app_modules, hookspath=[], hooksconfig={}, runtime_hooks=[], excludes=[], noarchive=False, optimize=0, )
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
exe = EXE( pyz, a.scripts, [], exclude_binaries=True, name='main', debug=False, bootloader_ignore_signals=False, strip=False, upx=True, console=True, disable_windowed_traceback=False, argv_emulation=False, target_arch=None, codesign_identity=None, entitlements_file=None, ) coll = COLLECT( exe, a.binaries, a.datas, strip=False, upx=True, upx_exclude=[], name='main', )
|