Python Relative import does not work from command line gives ValueError -
my directory structure follows
microblog/__init__.py urls.py views.py wsgi.py settings/__init__.py testing.py base.py local.py
in testing.py have relative import
from .base import * ... ...more code
when try run testing.py command line in directory microblog/settings using python testing.py
from .base import * valueerror: attempted relative import in non-package
why not work. settings directory valid package init.py . not valueerror command line if change
from .base import *
to
from base import *
i trying understand why relative local import fails , gives valueerror when run "testing.py" package relative import in command line.
the answer icyrock in this post clarifies didnt understand python "repl".
in directory microblog/settings when run
python testing.py
it puts testing in package "main" , not know testing part of package "settings". instead running "testing.py" module part of normal package hierarchy using this
python -m microblog.settings.testing
runs without valueerror since python knows "testing" part of package "settings" relative local import "from .base import *" makes perfect sense.
Comments
Post a Comment