首页 > 解决方案 > 找不到来自 pyinstaller“spec”文件的“datas”数组中的文件

问题描述

通过创建以下规范文件:

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None

face_models = [
('face_recognition_models/models/*.dat', 'face_recognition_models/models')
]

a = Analysis(['main.py'],
             pathex=[],
             binaries=face_models,
             datas=[
                ('material/haarcascade_frontalface_default.xml','tools'),
                ('processing/model_01_human_category.h5','tools'),
                ('icon/faceicon.ico','tools')
             ],
# MORE CODE...

在此处输入图像描述

我尝试运行创建的 .exe,但出现以下错误。

在此处输入图像描述

我的第 69 行:

self.model = load_model('tools/model_01_human_category.h5')

我承认我不知道还能做什么...

标签: pythonpyinstaller

解决方案


我找到了一个解决问题的有趣解决方案,点击链接:https ://github.com/explosion/spaCy/issues/3592

我使用此脚本从 .spec 构建的文件中获取正确的 PATH

def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath("__file__")))
    return os.path.join(base_path, relative_path)

推荐阅读