python3:ImportError:没有名为xxxx的模块[复制]

时间:2021-03-10 23:16:25

This question already has an answer here:

这个问题在这里已有答案:

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文件中报告了一个“Unresolved import:Pots”-error。我在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

1 个解决方案

#1


37  

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 Pots is part of the Phone package: there is no module named Pots, there's a module named Phone.Pots.

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

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

Python 2有一个称为相对导入的功能,即使在技术上不正确,也可以编写导入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.
  • 为什么这个模块被命名为Phone.Pots,但我可以使用import Pots?这是非常不一致的。

  • What if the submodule shadows a name of another module?
  • 如果子模块影响另一个模块的名称怎么办?

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__导入删除Python 2中的相对导入:

from __future__ import absolute_import

#1


37  

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 Pots is part of the Phone package: there is no module named Pots, there's a module named Phone.Pots.

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

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

Python 2有一个称为相对导入的功能,即使在技术上不正确,也可以编写导入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.
  • 为什么这个模块被命名为Phone.Pots,但我可以使用import Pots?这是非常不一致的。

  • What if the submodule shadows a name of another module?
  • 如果子模块影响另一个模块的名称怎么办?

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__导入删除Python 2中的相对导入:

from __future__ import absolute_import