从兄弟目录导入py会在非包错误

时间:2017-01-03 18:14:06

标签: python python-2.7 module

我一直在搜索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

1 个答案:

答案 0 :(得分:0)

您需要使用from project.helpers import functions,而您实际上并未将脚本用作程序包。有关详细信息,请与她联系:How to fix "Attempted relative import in non-package" even with __init__.py