Import Object Declared Inside __init__.py
Solution 1:
Generally speaking, I think it's best practice to have the data funnel up to __init__.py
from the modules/subpackages rather than needing to rely on data from __init__.py
in the surrounding modules. In other words, __init__.py
can use one.py
, but one.py
shouldn't use data/functions in __init__.py
.
Now, to your question...
It works in top
because python does a relative import (which is gone in python3.x IIRC, so don't depend on it ;-). In other words, python looks in the current directory for a module or package name lib
and it imports it. That's all fine so far. running from lib.one import a
first imports lib
(__init__.py
) which works fine. Then it imports one
-- lib
still imports ok from one
because it's relative to your current working directory -- Not relative to the source file.
When you move into the lib
directory, python can no longer find lib
in the current directory making it not importable. Note that with most packages, this is fixed by installing the package which puts it someplace that python can find it without it needing to be in the current directory.
Post a Comment for "Import Object Declared Inside __init__.py"