1

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
  • 28,901
  • 11
  • 96
  • 121
Rajnil Guha
  • 323
  • 1
  • 3
  • 14

1 Answers1

2

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
  • 13,233
  • 12
  • 67
  • 109