1

So I have directory with following structure:

> current_directory
    > submodule
        /__init__.py
        /some_module.py
    /main.py

In the __init__.py file the following statement is present:

from some_module import some_funciton

Whilst in the main.py the whole submodule is imported with: import submodule.

Now, this executed perfectly fine with Python 2, but the issue is that in Python 3 the import statement in __init__.py raises an ImportError exception: `No module named 'some_module'.

What is the difference between Python 2 and Python 3 when it comes to specifying the hierarchy of the imported files, and how would I go about adjusting the code to work with Python 3?

Bruno KM
  • 768
  • 10
  • 20

1 Answers1

2

In python 3 relative imports are supported only in the form from . import submodule.

You should either rewrite your import statement or make the import absolute by adding project directory to python path:

export PYTHONPATH=current_directory
python main.py
Yossi
  • 11,778
  • 2
  • 53
  • 66
  • Thanks, that clears it up! Someone linked an answer to a different question that included this link: https://www.python.org/dev/peps/pep-0328/ . Which was also very helpful. – Bruno KM Jul 31 '17 at 12:22