【问题标题】:Importing py from sibling directory gives Attempted relative import in non-package error从同级目录导入 py 会在非包错误中尝试相对导入
【发布时间】:2017-05-17 22:04:03
【问题描述】:

我一直在 stackoverflow 上搜索,但找不到问题所在。

我的目录结构如下:

--project
  --__init__.py
  --helpers
    --__init__.py
    --functions.py
  --set1
    --__init__.py
    --foo.py
  --set2
    --__init__.py
  --setn
    --__init__.py

foo.py我要导入functions.py 我这样做是用

from ..helpers import functions

这给了我错误:

Attempted relative import in non-package error

到目前为止,我找到的所有答案都是关于正确设置 init.py。

在 Mac 上使用 python2.7

【问题讨论】:

标签: python python-2.7 module


【解决方案1】:

您需要使用from project.helpers import functions,您实际上并没有将您的脚本用作一个包。更多信息请见她:How to fix "Attempted relative import in non-package" even with __init__.py

【讨论】:

    猜你喜欢
    • 2017-03-22
    • 1970-01-01
    • 2020-03-07
    • 1970-01-01
    • 2020-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-28
    相关资源
    最近更新 更多