即使使用__init__.py,也如何解决“尝试以非软件包方式进行相对导入”

≡放荡痞女 提交于 2020-07-27 15:16:55

问题:

I'm trying to follow PEP 328 , with the following directory structure: 我正在尝试使用以下目录结构来遵循PEP 328

pkg/
  __init__.py
  components/
    core.py
    __init__.py
  tests/
    core_test.py
    __init__.py

In core_test.py I have the following import statement core_test.py我有以下导入语句

from ..components.core import GameLoopEvents

However, when I run, I get the following error: 但是,当我运行时,出现以下错误:

tests$ python core_test.py 
Traceback (most recent call last):
  File "core_test.py", line 3, in <module>
    from ..components.core import GameLoopEvents
ValueError: Attempted relative import in non-package

Searching around I found " relative path not working even with __init__.py " and " Import a module from a relative path " but they didn't help. 到处搜索时,我发现“ 即使使用__init__.py,相对路径也不起作用 ”和“ 从相对路径导入模块 ”,但它们没有帮助。

Is there anything I'm missing here? 我在这里想念什么吗?


解决方案:

参考一: https://stackoom.com/question/mPEq/即使使用-init-py-也如何解决-尝试以非软件包方式进行相对导入
参考二: https://oldbug.net/q/mPEq/How-to-fix-Attempted-relative-import-in-non-package-even-with-init-py
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!