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
  • 121,510
  • 29
  • 395
  • 339
Tomas T
  • 449
  • 1
  • 4
  • 17

1 Answers1

1

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

#second.py
from . import first
if __name__=='__main__':
    first.foo()
Ali
  • 2,541
  • 2
  • 17
  • 31