Python 和 Pyinstaller - 如何导入资源文件?

Python and Pyinstaller - How to import resource files?

我在 python3.6 中有一个非常简单的项目(我无法更改 python 版本):

resources/
  __init__.py
  file.txt
start.py

with start.py 包含:

import importlib_resources
import resources

with importlib_resources.path(resources, 'file.txt') as p:
  with open(p) as file:
    print(file.read())

file.txt 包含“Hello Word”,当 运行 python start.py

时正确打印这句话

但在使用 pyinstaller 打包后,具有以下规范文件:

# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['start.py'],
         pathex=['D:\dev\ec'],
         binaries=[],
         datas=[('resources','resources')],
         hiddenimports=[],
         hookspath=[],
         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='start',
      debug=False,
      bootloader_ignore_signals=False,
      strip=False,
      upx=True,
      console=True )
coll = COLLECT(exe,
           a.binaries,
           a.zipfiles,
           a.datas,
           strip=False,
           upx=True,
           upx_exclude=[],
           name='start')

资源文件被复制到 pyinstaller dist 文件夹中(如此处解释Pyinstaller: How to include resources from package used by importlib_resources)但是我在执行生成的 exe 文件时出现此错误:

PS D:\dev\ec> .\dist\start\start.exe
Traceback (most recent call last):
 File "start.py", line 4, in <module>
 with importlib_resources.path(resources, 'file.txt') as p:
 File "contextlib.py", line 81, in __enter__
 File "importlib_resources\_common.py", line 87, in _tempfile
 File "tempfile.py", line 342, in mkstemp
 File "tempfile.py", line 258, in _mkstemp_inner
TypeError: must be str, not method
[8148] Failed to execute script start

有人能帮忙吗? tks

最后我摆脱了 importlib_resources 并使用 pkg_resources 代替它,它就像一个魅力:

import pkg_resources
with open(pkg_resources.resource_filename('resources', 'file.txt')) as file:
    print(file.read())