问题
I'm having trouble understanding how objects declared inside '__init__.py' are/should be imported to other files.
I have a directory structure like so
top/
|
|_lib/
|_ __init__.py
|_ one.py
File contents are as follows
lib/__init__.py
a=object()
lib/one.py
from lib import a
Here is the problem. If I fire a python shell from top
directory, the following command runs well
>>> from lib.one import a
However if I change directory to top/lib
and fire a similar command in a new python shell, I get error.
>>> from one import a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "one.py", line 1, in <module>
from lib import a
ImportError: No module named lib
Ofcourse, I can change one.py
like so, that will make everything work.
from __init__ import a
But I'm really trying to understand, why import command works from top
directory and not from top/lib
.
Thanks.
回答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.
来源:https://stackoverflow.com/questions/26190511/import-object-declared-inside-init-py