python3: ImportError: No module named xxxx


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)

     |--- Phone
     |      |---
     |      |---

cat ./jmLib2/

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

cat ./jmLib2/
from Pots import Pots

cat ./
from Phone import Pots

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


When I now do:

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

GREAT…BUT, if I do:

Traceback (most recent call last):
  File "", line 2, in <module>
    from Phone import Pots
  File "/home/juergen/my/Python/jmLib2/Phone/", 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 file an “Unresolved import: Pots”-error.
I have the same traceback-problem under PyDev and 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?


Asked By: MatrixClient



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

Either use:

from Phone.Pots import Pots


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
Answered By: Andrea Corbellini
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.