简体   繁体   English

尝试使用python -m超出顶级包的相对导入

[英]Attempted relative import beyond top level package using python -m

I've looked at most every link regarding relative imports and especially those with regard to top-level-packages, but I'm still having a ton of trouble getting my relative import code to work. 我已经查看了大多数与相对导入有关的链接,尤其是与顶级程序包有关的链接,但是要使我的相对导入代码正常工作仍然很麻烦。 For reference, I'm using Python 3.6. 供参考,我使用的是Python 3.6。

I have a directory of tests that I want to run, isolated in their own directory, tests. 我有一个要运行的测试目录,隔离在自己的目录中。 I want to import all of the modules package into one file called InputTests.py. 我想将所有模块包导入一个名为InputTests.py的文件中。 Here is the format of my project directory. 这是我的项目目录的格式。 文件目录

Within InputTests.py, I attempt to import modules this way: 在InputTests.py中,我尝试以这种方式导入模块:

from .. import modules

I get the error: 我得到错误:

File "/Users/wfehrnstrom/Desktop/meeting_minutes/tests/InputTests.py", 
line 2, in <module>
from .. import modules
ValueError: attempted relative import beyond top-level package<code>

However, I'm using the command: python -m tests.InputTests , which supposedly tells the interpreter to run everything from my top level directory. 但是,我使用的命令是: python -m tests.InputTests ,它应该告诉解释器运行顶层目录中的所有内容。 So I guess my question is, why does my relative import statement not work given the fact that I am running this from the package above. 所以我想我的问题是,考虑到我是从上面的包中运行它的,为什么我的相对导入语句不起作用。 This stack overflow post seemed to detail what I need, but their solution, running with -m doesn't work for me: How to do relative imports in Python? 这篇堆栈溢出文章似乎详细说明了我需要的内容,但是用-m运行的他们的解决方案对我不起作用: 如何在Python中进行相对导入?

In addition, there seems to be a contradiction to this in this stack overflow post: Relative importing modules from parent folder subfolder 此外,在此堆栈溢出文章中似乎与此矛盾: 从父文件夹子文件夹相对导入模块

The latter post seems to suggest that the contextual meaning of .. and . 后者似乎暗示了..和。的上下文含义。 do not change based on where you execute your Python command, however the former says that it does. 不会根据执行Python命令的位置而改变,但是前者说确实如此。 This has me extremely confused. 这让我非常困惑。 Can anyone both clarify relative imports and resolve this discrepancy? 任何人都可以澄清相对进口并解决这一差异吗? Thank you. 谢谢。

I can only try to answer your question for Python 2.7 but I hope this is helpful anyways. 我只能尝试回答您对Python 2.7的问题,但是我仍然希望这对您有所帮助。

PEP 328 which is quoted in the answer to "How to do relative imports in Python?" 在“如何在Python中进行相对导入?”的答案中引用了PEP 328。 you linked is partwise outdated since it suggests importing based on the __name__ of the module which would be __main__ for the main script and therefore prevent using relative imports in that module at all even when using -m . 您链接的部分已过时,因为它建议基于模块的__name__进行导入,对于主脚本而言,该名称为__main__ ,因此即使使用-m ,也完全禁止在该模块中使用相对导入。

PEP 366 solved this by introducing the __package__ attribute. PEP 366通过引入__package__属性解决了这一问题。

When you use python -m tests.InputTests the value of the __package__ in InputTests.py will be tests . 当您使用python -m tests.InputTests时, __package__InputTests.py的值将为tests This does not allow importing from parent packages since tests is the current and top-level package. 这不允许从父程序包导入,因为tests是当前的顶层程序包。

Running python -m modules.tests.InputTests in the directory "meeting_minutes" should solve that. 在目录“ meeting_minutes”中运行python -m modules.tests.InputTests应该可以解决该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM