打包报错记录2

上一集

前置知识

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

项目结构

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

do_segment调用了nnUnet的方法(所以使得打包变得复杂了起来)
在这里插入图片描述
这里引用包,采用一种神奇的方法
打包后,路径有发生变化,所以不能用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')

难受啊,他搜不到可还行
在这里插入图片描述
经过https://pyinstaller.readthedocs.io/en/stable/usage.html
这个-p可以添加搜索路径
在这里插入图片描述

第二次打包

这里与上一个的区别是: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,这个简单啊
在这里插入图片描述

第三次打包

设置
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')

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

在这里插入图片描述
搜索路径修改为绝对路径,原本的路径毛都搜不到
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:你妈
在这里插入图片描述
参考这里
https://github.com/pyinstaller/pyinstaller/wiki/Recipe-Multiprocessing

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

看上去是正常的
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Nightmare004

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值