Python import mechanics

The first thing you should know is that the Python language is NOT an ISO standard. This is rather different from C/C++, and it means that there’s no “proper” way to define a language behaviour – CPython might do something just because it was coded that way, and Jython might do the other way round.

about your questions, remember that “importing” a module is a two-part operation: first the module is loaded – if it had never been, e.g. if it wasn’t available in sys.modules, then a name is bound to that module in the local namespace.

hence:

1) Yes, you can reference whatever you want from module a by providing the proper namespace, e.g. you’ll have to do something like

B.C.name = “something”

And I think this is very rarely done in Python programs and could be considered bad practice since it forces a “transitive dep” – if some module B implementation is refactored and doesn’t depend on C anymore, it should continue offering the C module just for satisfying A deps.

Of course setting __ all __ can prevent this, and a good practice may be to put __ all __ in all your modules, and export just the symbols you want to be really public.

2) Yes and no. Doing

import a.b.c.d 

performs the first import phase (loading) on all modules, but the second just on a (and, recursively, in b with respect to c, etc) but all the modules in the chain must be referenced by full namespace; after such an import, you can do

a.something
a.b.something
a.b.c.something

but you can’t do

c.something
b.something

I must admit that kind of usage is pretty rare as well; I generally prefer the “from module import something” way-to-import, and generally you just ask for what you need – such nesting is neither common in libraries, nor its usage is that common.

Many times there’re “outer packages”, just used for organization, which hold modules with classes. It’s very likely that a, b, c above are just packages, and d is a module which truly holds classes, functions and other objects. So the proper usage would be:

from a.b.c.d import name1, name2, name3

I hope this satifies your curiosity.

Leave a Comment