Relative imports use a module's __name__ attribute to determine that module's position in the package hierarchy. If the module's name does not contain any package information (e.g. it is set to '__main__') then relative imports are resolved as if the module were a top level module, regardless of where the module is actually located on the file system.在 Python 2.6 中,他们添加了相对于主模块引用模块的功能.PEP 366 描述了这一变化.In Python 2.6, they're adding the ability to reference modules relative to the main module. PEP 366 describes the change.更新:根据 Nick Coghlan 的说法,推荐的替代方法是使用 -m 开关在包内运行模块.Update: According to Nick Coghlan, the recommended alternative is to run the module inside the package using the -m switch. 这篇关于如何在 Python 中进行相对导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!