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
128k31 gold badges417 silver badges348 bronze badges
asked Aug 5, 2018 at 4:07
0

1 Answer 1

1

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

#second.py
from . import first
if __name__=='__main__':
 first.foo()
answered Aug 5, 2018 at 4:15
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.