4

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.

Unihedron
11.1k13 gold badges65 silver badges72 bronze badges
asked Oct 4, 2014 at 7:05

1 Answer 1

6

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.

answered Oct 4, 2014 at 7:14
Sign up to request clarification or add additional context in comments.

1 Comment

That makes lot of sense. Thanks

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.