Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ModuleNotFoundError in Python 3 but not 2

I am getting a ModuleNotFoundError in Python 3 when trying to import a package containing an __init__.py that imports a variable from one of the package's modules.

My project structure is:

project/
  test.py
  package/
    __init__.py
    modu.py

modu.py:

value = 99

__init__.py:

from modu import value

test.py:

import package
print(package.value)  # or 'print package.value' for Python 2

When I run test.py using Python 2, everything works. But when I run with Python 3, I get a ModuleNotFoundError: No module named 'modu'. I am running from the project/ directory.

Can anyone explain why this is happening? Thanks.

like image 800
Khanh Do Avatar asked May 30 '18 10:05

Khanh Do


Video Answer


1 Answers

Because Python3 expect absolute path for modules:

__init__.py:

from package.modu import value

Works on both versions

like image 139
Arount Avatar answered Nov 09 '22 18:11

Arount