python3:导入错误:没有名为 xxxx 的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34461987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:00:59  来源:igfitidea点击:

python3: ImportError: No module named xxxx

pythonpackageimporterror

提问by MatrixClient

I am new to Python and I am trying to understand a problem, which I see when creating a package. I have the following file structure: (Working-Directory is /my/Python/jmLib2)

我是 Python 新手,我试图理解一个问题,我在创建包时看到了这个问题。我有以下文件结构:(工作目录是/my/Python/jmLib2)

/my/Python/jmLib2
     |--- Phone
     |      |--- __init__.py
     |      |--- Pots.py
     |- Test2.py

---------------------------------
cat ./jmLib2/Pots.py
#!/usr/bin/python

def Pots():
    print ("I'm Pots Phone")

---------------------------------
cat ./jmLib2/__init__.py
from Pots import Pots

---------------------------------
cat ./Test2.py
#!/usr/bin/python
from Phone import Pots

import os.path
print ("OS:"+str(os.path))

Pots()


When I now do:

当我现在做:

python2 Test2.py
OS:<module 'posixpath' from '/usr/lib/python2.7/posixpath.pyc'>
    I'm Pots Phone*

GREAT...BUT, if I do:

很好……但是,如果我这样做:

python3 Test2.py
Traceback (most recent call last):
  File "Test2.py", line 2, in <module>
    from Phone import Pots
  File "/home/juergen/my/Python/jmLib2/Phone/__init__.py", line 1, in <module>
    from Pots import Pots
ImportError: No module named 'Pots'

I am working with PyDev under Eclipse. PyDev reports me inside the init.py file an "Unresolved import: Pots"-error. I have the same traceback-problem under PyDev and bash.

我正在 Eclipse 下使用 PyDev。PyDev 在init.py 文件中向我报告“未解析的导入:Pots”错误。我在 PyDev 和 bash 下有相同的回溯问题。

Again, I am new to Python... so it is maybe a very stupid mistake. But can someone explain me, the difference between python2 and python3.4? Do I have to modify the PYTHONPATH? Why?

同样,我是 Python 的新手……所以这可能是一个非常愚蠢的错误。但是有人可以解释一下,python2 和 python3.4 之间的区别吗?我必须修改 PYTHONPATH 吗?为什么?

Greetings Juergen

问候尤尔根

采纳答案by Andrea Corbellini

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

TL;DR:相对进口消失了。请改用绝对导入。

Either use:

要么使用:

from Phone.Pots import Pots

or:

或者:

from .Pots import Pots


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

出现问题是因为PotsPhone包的一部分:没有名为 的模块Pots,有一个名为 的模块Phone.Pots

Python 2 had a feature called relative importsthat let you write import Potseven if that was not technically correct.

Python 2 有一个称为相对导入的功能,import Pots即使这在技术上不正确,也可以让您编写。

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?
  • 阅读代码的人无法立即说出导入是否来自包。
  • 为什么模块被命名Phone.Pots,但我可以使用import Pots?这是非常不一致的。
  • 如果子模块隐藏了另一个模块的名称怎么办?

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

由于这些原因,从 Python 3 中删除了相对导入。



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

您可以使用__future__import摆脱 Python 2 的相对导入

from __future__ import absolute_import