【问题标题】:how to import files properly when __init__.py breaks pyinstaller__init__.py 中断 pyinstaller 时如何正确导入文件
【发布时间】:2019-01-03 21:59:35
【问题描述】:

我已经解决了pyinstaller 的问题,我的问题的解决方法是从我的树中删除空的__init__.py 文件,因此我不再拥有该文件并且一切都按预期工作。但是,现在我正在添加更多文件并具有不同的文件夹结构:

dist
|
+--- rating_service.exe  # created by pyinstaller
service
|
+--- rating_service.py
shared
|
+--- resource_globals.py

在 rating_service.py 中,我尝试了这些导入并得到这些错误:

from . import shared

ImportError: 无法导入名称“共享”

from .. import shared

ValueError: 尝试相对导入超出顶级包

import shared

ModuleNotFoundError: 没有名为“共享”的模块

如何在我的rating_service 中访问我的resource_globals 内容?

【问题讨论】:

    标签: python python-3.x python-import pyinstaller


    【解决方案1】:

    如果添加任何__init__.py 文件不可行,那么作为一种解决方法,您可以在rating_service.py 代码文件中执行:

    # -----------------
    # rating_service.py
    # -----------------
    import os
    import sys
    
    # Manually add the 'shared' directory to the python search paths
    file_dir = os.path.dirname(os.path.realpath(__file__))
    shared_dir = os.path.join(file_dir, '../shared')
    sys.path.insert(0, shared_dir)
    
    import resource_globals
    

    【讨论】:

      猜你喜欢
      • 2019-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-29
      • 1970-01-01
      • 1970-01-01
      • 2019-02-27
      • 1970-01-01
      相关资源
      最近更新 更多