这里似乎已经有了一些关于python 3中相对导入的问题,但在浏览了许多之后,我仍然没有找到我的问题的答案。 问题来了。

我有一个如下所示的包

package/
   __init__.py
   A/
      __init__.py
      foo.py
   test_A/
      __init__.py
      test.py

我在test.py中有一行:

from ..A import foo

现在,我在包的文件夹,我运行

python -m test_A.test

我收到消息

"ValueError: attempted relative import beyond top-level package"

但是如果我在包的父文件夹中,例如,我运行:

cd ..
python -m package.test_A.test

一切都很好。

现在我的问题是: 当我在包的文件夹中,我运行test_A子包中的模块作为test_A。测试,根据我的理解,..A只上升了一层,仍然在包文件夹中,为什么它给出消息说在顶层包之外。导致此错误消息的确切原因是什么?


当前回答

只需移除…在test.py 对我来说,pytest很好地解决了这个问题 例子:

from A import foo

其他回答

假设: 如果您在包目录中,则A和test_A是单独的包。

结论: ..只允许在包中导入。

进一步指出: 如果您想强制包可以放置在sys.path中的任何路径上,那么使相对导入仅在包中可用是很有用的。

编辑:

我是唯一一个认为这很疯狂的人吗?为什么当前的工作目录不被认为是一个包?——Multihunter

当前工作目录通常位于sys.path中。所有文件都是可导入的。这是自Python 2以来的行为,当时包还不存在。将运行目录作为一个包将允许以“import a”和“import a”的方式导入模块,这将是两个不同的模块。也许这是需要考虑的不一致性。

import sys
sys.path.append("..") # Adds higher directory to python modules path.

试试这个。 为我工作。

如果在上层文件夹中有__init__.py,可以将导入初始化为 导入文件/路径作为初始化文件的别名。然后你可以使用它在较低的脚本:

import alias

编辑:这个问题在其他问题中有更好/更连贯的答案:

兄弟包导入 第10亿次相对进口


Why doesn't it work? It's because python doesn't record where a package was loaded from. So when you do python -m test_A.test, it basically just discards the knowledge that test_A.test is actually stored in package (i.e. package is not considered a package). Attempting from ..A import foo is trying to access information it doesn't have any more (i.e. sibling directories of a loaded location). It's conceptually similar to allowing from ..os import path in a file in math. This would be bad because you want the packages to be distinct. If they need to use something from another package, then they should refer to them globally with from os import path and let python work out where that is with $PATH and $PYTHONPATH.

当你使用python -m package.test_A。测试,然后使用from ..import foo可以很好地解决问题,因为它跟踪了包中的内容,而你只是访问了加载位置的子目录。

为什么python不认为当前工作目录是一个包?不知道,但天哪,这将是有用的。

从包中。一个import foo

我认为这比

import sys
sys.path.append("..")