So I have a very simple package with the following structure:
package/
__init__.py
a.py
subpackage/
__init__.py
b.py
a.py:
print __name__
from .subpackage.b import *
b.py
print __name__
1) Why if I run python -m a
from when inside package it returns: ValueError: Attempted relative import in non-package
. But If I leave the package directory and I run python -m package.a
it works and prints:
__main__
package.subpackage.b
And also, why does it say __main__
as __name__
for a.py if I am running it as a module with-m
option?
2) Why if then I write a c.py
module that I put outside package that just imports from package import a
I get:
package.a
package.subpackage.b
So I understand that now __name__
from a is not __main__
but why is that the case when I run it with as a module with python -m
option. And why if I place that same main.py
inside package, then the attempted relative import error appears again?