繁体   English   中英

Pyinstaller:如何从 importlib_resources 使用的包中包含资源

[英]Pyinstaller: How to include resources from package used by importlib_resources

我有以下项目结构:

package1/
  __init__.py
  some.py
  package2
    __init__.py
    some.py
    static_data/
      __init__.py
      file1.txt
      file2.txt
      ...
my_script.py

my_script.py包含来自整个结构的导入。 除了importlib_resources使用之外,python 代码也很好。

我使用importlib_resources (Python 3.6) 访问文件, importlib_resources所示: importlib_resources.open_text(static_data, 'file1.txt').readlines()

构建可执行文件: pyinstaller my_script.py -F --noconsole --noupx

在结果可执行文件中,我收到以下错误:

File "lib\site-packages\importlib_resources\_py3.py", line 62, in open_text
File "lib\site-packages\importlib_resources\_py3.py", line 52, in open_binary
FileNotFoundError: 'file1.txt' resource not found in 'package1.package2.static_data'

包含与importlib_resources使用的资源的正确方法是什么?

默认情况下,PyInstaller 不会将非二进制文件添加到生成的二进制文件中。 您可以手动添加使用--add-data选项,说明在这里

假设您正在运行的窗口和您正在访问的资源文件都在package1顶级包中,您可以尝试这样的事情:

pyinstaller my_script.py -F --noconsole --noupx --add-data "package1;package1"

在脚本中需要数据

import platform
if platform.sys.version.split()[0]>='3.7':
    import importlib.resources as importlib_resources
else:
    import importlib_resources

settings = json.loads(importlib_resources.read_text('packagename','data.json'))

在 setup.py 中

install_requires=[
        'importlib_resources ; python_version<"3.7"'
      ],

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM