2

I have a package as follows:

mypackage/
     __init__.py
     mod1.py
     mod2.py

Inside mod1.py I have a definition called calculate(). Now I am writing this code in __init__.py from mod1 import calculate

But everytime I write this code I am getting an error saying

unresolved import calculate.

I am not able to understand why I am getting this error.

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
Rajnil Guha
  • 425
  • 1
  • 4
  • 15

2 Answers2

3

You should import your modules from the package root folder as follows:

from mypackage.mod1 import calculate

Python uses the package name (e.g mypackage in your case) as a namespace, in which, it will look for the module mod1. Since you didn't tell python the namespace it didn't know where to look for your module, therefore yielded the error:

unresolved import calculate

The python documentation explains that in details here. You can also check this SO thread for a detailed explanation on how package imports work.

Mohamed Ali JAMAOUI
  • 14,275
  • 14
  • 73
  • 117
0

In your __init__.py, you can import module using '.'

from .mod1 import calculate
bhargav3vedi
  • 521
  • 1
  • 6
  • 11