【问题标题】:Python and Pyinstaller - How to import resource files?Python 和 Pyinstaller - 如何导入资源文件?
【发布时间】:2021-06-17 16:06:52
【问题描述】:

我在python3.6中有一个相当简单的项目(我不能更改python版本):

resources/
  __init__.py
  file.txt
start.py

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

【问题讨论】:

    标签: python-3.x resources pyinstaller python-importlib


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-09
      • 1970-01-01
      • 2023-01-20
      • 2018-12-28
      • 2011-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多