python3: ImportError: No module named xxxx [duplicate]

喜欢而已 提交于 2019-11-30 01:26:04

TL;DR: Relative imports are gone. Use absolute imports instead.

Either use:

from Phone.Pots import Pots

or:

from .Pots import Pots

The problem occurs because Pots is part of the Phone package: there is no module named Pots, there's a module named Phone.Pots.

Python 2 had a feature called relative imports that let you write import Pots even if that was not technically correct.

Relative imports however are a source of problems and confusion:

  • Who reads the code cannot immediately say whether the import is from a package or not.
  • How come the module is named Phone.Pots, but I can use import Pots? This is highly inconsistent.
  • What if the submodule shadows a name of another module?

For these reasons, relative imports were removed from Python 3.


You can get rid of relative imports from Python 2 by using a __future__ import:

from __future__ import absolute_import
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!