打包报错记录2

上一集

前置知识

sys.path是一个包含了python搜索路径的列表,如果这个列表都找不到这个模块,就g了

项目结构

项目结构大概是这样的(忽略build和dist)
启动项在multi_window.py
中间的nnUNet链接https://github.com/MIC-DKFZ/nnUNet
nnUnet没动过,所以他的import并不是从项目名字(visualizer_pyqt5)开始的

do_segment调用了nnUnet的方法(所以使得打包变得复杂了起来)
打包报错记录2_第1张图片
这里引用包,采用一种神奇的方法
打包后,路径有发生变化,所以不能用sys.path.append(’…’)这种方法

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
import os
import sys

if getattr(sys, 'frozen', None):
    base_dir = sys._MEIPASS
else:
    base_dir = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.join(base_dir, 'models', 'nnUNet'))

第一次打包

初代spec文件

# -*- mode: python -*-
# _*_ coding:utf-8 _*_
import os
import sys


block_cipher = None

# 当前路径
cur_path=os.path.abspath('.')
# python所在的路径
python_path=sys.path[4]
# 第三方包路径
packages_path=os.path.join(python_path,'Lib','site-packages')


block_cipher = None

# 需要的数据
datas=[
(os.path.join('resources','*'),'resources')
]

for model in models:
    if os.path.isabs(model):
        model = os.path.abspath(model)
    model = os.path.relpath(model, cur_path)
    for root, dirs, files in os.walk(model):
        for file in files:
            datas.append((os.path.join(root, file), root))

a = Analysis(['multi_window.py'],
             pathex=[
             ],
             binaries=[],
             datas=datas,
             hiddenimports=[],
             hookspath=[],
             hooksconfig={},
             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, 
          [],
          exclude_binaries=True,
          name='multi_window',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=True,
          disable_windowed_traceback=False,
          target_arch=None,
          codesign_identity=None,
          entitlements_file=None )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas, 
               strip=False,
               upx=True,
               upx_exclude=[],
               name='multi_window')

难受啊,他搜不到可还行
打包报错记录2_第2张图片
经过https://pyinstaller.readthedocs.io/en/stable/usage.html
这个-p可以添加搜索路径
打包报错记录2_第3张图片

第二次打包

这里与上一个的区别是:pathex加了那个找不到的nnUnet的路径

# -*- mode: python -*-
# _*_ coding:utf-8 _*_
import os
import sys


block_cipher = None

# 当前路径
cur_path=os.path.abspath('.')
# python所在的路径
python_path=sys.path[4]
# 第三方包路径
packages_path=os.path.join(python_path,'Lib','site-packages')


block_cipher = None

# 需要的数据
datas=[
(os.path.join('resources','*'),'resources')
]

for model in models:
    if os.path.isabs(model):
        model = os.path.abspath(model)
    model = os.path.relpath(model, cur_path)
    for root, dirs, files in os.walk(model):
        for file in files:
            datas.append((os.path.join(root, file), root))

a = Analysis(['multi_window.py'],
             pathex=[
                os.path.join(cur_path, 'models', 'nnUNet')
             ],
             binaries=[],
             datas=datas,
             hiddenimports=[],
             hookspath=[],
             hooksconfig={},
             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, 
          [],
          exclude_binaries=True,
          name='multi_window',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=True,
          disable_windowed_traceback=False,
          target_arch=None,
          codesign_identity=None,
          entitlements_file=None )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas, 
               strip=False,
               upx=True,
               upx_exclude=[],
               name='multi_window')

提示没有sklearn.utils._typedefs,这个简单啊
打包报错记录2_第4张图片

第三次打包

设置
hiddenimports=[‘sklearn.utils._typedefs’]

# -*- mode: python -*-
# _*_ coding:utf-8 _*_
import os
import sys


block_cipher = None

# 当前路径
cur_path=os.path.abspath('.')
# python所在的路径
python_path=sys.path[4]
# 第三方包路径
packages_path=os.path.join(python_path,'Lib','site-packages')


block_cipher = None

# 需要的数据
datas=[
(os.path.join('resources','*'),'resources')
]

for model in models:
    if os.path.isabs(model):
        model = os.path.abspath(model)
    model = os.path.relpath(model, cur_path)
    for root, dirs, files in os.walk(model):
        for file in files:
            datas.append((os.path.join(root, file), root))

a = Analysis(['multi_window.py'],
             pathex=[
                os.path.join(cur_path, 'models', 'nnUNet')
             ],
             binaries=[],
             datas=datas,
             hiddenimports=['sklearn.utils._typedefs'],
             hookspath=[],
             hooksconfig={},
             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, 
          [],
          exclude_binaries=True,
          name='multi_window',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=True,
          disable_windowed_traceback=False,
          target_arch=None,
          codesign_identity=None,
          entitlements_file=None )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas, 
               strip=False,
               upx=True,
               upx_exclude=[],
               name='multi_window')

啊这
打包报错记录2_第5张图片
这nnunet真是= =
这个nnunet.path[0]其实有点问题,可能搜索不到
打包报错记录2_第6张图片
这个recursive_find_python_class非常的神奇
会莫名其妙找一些不存在的包,然后import,然后报错,还不try except
所以我的做法
加try except

打包报错记录2_第7张图片
搜索路径修改为绝对路径,原本的路径毛都搜不到
base_dir的定义和上面的一样
在这里插入图片描述
保险起见,我在init.py里
从from . import *改成了from . import xxx因为不知道为啥,有时候搜索不到一些包

第四次打包

注意os.walk(‘models/nnUNet/nnunet’)
为了能让他顺利搜索,我把py文件全部拷过去了

# -*- mode: python -*-
# _*_ coding:utf-8 _*_
import os
import sys

from PyInstaller.utils.hooks import collect_submodules


block_cipher = None

# 当前路径
cur_path=os.path.abspath('.')
# python所在的路径
python_path=sys.path[4]
# 第三方包路径
packages_path=os.path.join(python_path,'Lib','site-packages')


block_cipher = None

models=[
    'models/nnUNet/nnUNetTrainerV2__nnUNetPlansv2.1'
]

# 需要的数据
datas=[
(os.path.join('resources','*'),'resources')
]

hidden_imports = ['sklearn.utils._typedefs']

for model in models:
    if os.path.isabs(model):
        model = os.path.abspath(model)
    model = os.path.relpath(model, cur_path)
    for root, dirs, files in os.walk(model):
        for file in files:
            datas.append((os.path.join(root, file), root))

for root, dirs, files in os.walk('models/nnUNet/nnunet'):
    for file in files:
        if file.endswith('.py') and not file.startswith('__'):
            datas.append((os.path.join(root, file), root))

a = Analysis(['multi_window.py'],
             pathex=[
                os.path.join(cur_path, 'models', 'nnUNet')
             ],
             binaries=[],
             datas=datas,
             hiddenimports=hidden_imports,
             hookspath=['models/nnUNet/nnunet/training/network_training'],
             hooksconfig={},
             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, 
          [],
          exclude_binaries=True,
          name='multi_window',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=True,
          disable_windowed_traceback=False,
          target_arch=None,
          codesign_identity=None,
          entitlements_file=None )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas, 
               strip=False,
               upx=True,
               upx_exclude=[],
               name='multi_window')

启动
礼貌Nightamre:你妈
打包报错记录2_第8张图片
参考这里
https://github.com/pyinstaller/pyinstaller/wiki/Recipe-Multiprocessing

if __name__=='__main__':
	# 在此处添加
	multiprocessing.freeze_support()
	# 这里是你的代码
	# ......

看上去是正常的
打包报错记录2_第9张图片

你可能感兴趣的:(安装与配置,python,深度学习)