2

For example I have this package:

└── package
   │   __init__.py
   │   first.py
   │   second.py

and in my first.py

#first.py

def foo(): pass

in second.py

#second.py
from .first import foo
if __name__=='__main__':
    foo()

Now if i try to execute the second.py as:

$ cd package
$ python3 second.py 

I got this error:

ModuleNotFoundError: No module named '__main__.first'; '__main__' is not a package

why does this happen?

davidism
  • 110,080
  • 24
  • 357
  • 317
Tomas T
  • 419
  • 1
  • 3
  • 14

1 Answers1

1

You should add first.py to second.py without dot

#second.py
from . import first
if __name__=='__main__':
    first.foo()
Mohammad Ali
  • 2,303
  • 1
  • 14
  • 30