2

I have the following files:

./main.py
./lib/__init__.py
./lib/lib.py

,

$ cat lib/lib.py
def method():
 return 'method'

,

$ cat lib/__init__.py
from .lib import *

,

$ cat main.py
import lib
def main():
 print(lib.lib)
if __name__=='__main__':
 main()

I don't know why lib.lib is defined, it's not a variable in the lib.py file. Any ideas?

asked Jul 3, 2013 at 12:26
2
  • It looks like you are trying to reference the lib.py file when you should be trying to use the functionality from the lib file. i.e. lib.somelibfunction()... Commented Jul 3, 2013 at 12:29
  • You have two cat main.py lines; I think the first one should really be cat lib/lib.py instead. Commented Jul 3, 2013 at 12:30

1 Answer 1

3

The lib.lib object is the nested lip.py file inside the lib package.

Once you have imported the sub module, it also becomes available as an attribute of the package (unless you also included a lib name in the __init__.py file.

The Python 3.3 module loader (which is implemented in Python) simply sets a nested module as an attribute on the parent:

# [find a loader and load the module object, setting sys.modules[name]]
module = sys.modules[name]
if parent:
 # Set the module as an attribute on its parent.
 parent_module = sys.modules[parent]
 setattr(parent_module, name.rpartition('.')[2], module)

This behaviour is more or less implied in the Python packages documentation:

All modules have a name. Subpackage names are separated from their parent package name by dots, akin to Python’s standard attribute access syntax. Thus you might have a module called sys and a package called email, which in turn has a subpackage called email.mime and a module within that subpackage called email.mime.text.

answered Jul 3, 2013 at 12:30
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks, can you point out to me where in the docs this behavior is specified?
@simonzack read up on relative imports, also packages
@simonzack: I suspect it may be documented in python.org/doc/essays/packages.html but I am still scanning that.
@MartijnPieters this part, perhaps? "Whenever a submodule of a package is loaded, Python makes sure that the package itself is loaded first, loading its __init__.py file if necessary. The same for packages. Thus, when the statement import Sound.Effects.echo is executed, it first ensures that Sound is loaded; then it ensures that Sound.Effects is loaded; and only then does it ensure that Sound.Effects.echo is loaded (loading it if it hasn't been loaded before)." Not sure if that differs in Python 3, though.
@JAB: No. The part where lib.__dict__['lib'] is being set to support the expectation that lib.lib should work even when not explicitly imported under that name in lib/__init__.py.
|

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.