import packet.module without importing packet.__init__ ?
steve+comp.lang.python at pearwood.info
Sun Sep 11 06:51:18 CEST 2011
Gelonida N wrote:
> There's still something, that I am philosophycally missing.
> Wy do I have to import the entire tree if I'm just interested in a leave.
You don't. Python just imports the branches leading to the leaf, not the
> But if I know that I just want to boil some eggs I would really
> appreciate if I could just say
>> from kitchen.pot import boil
> without having the side effect of getting pans and knives also loaded
> into memory.
In your example, you stated that kitchen explicitly imports kitchen.pans and
kitchen.knives. So in that case, take it up with the designer of the
kitchen package -- it was his decision to import them, just like he
imported re and math and httplib. (For example.)
If kitchen is your package, then it is your choice: if you want to have
control over when sub-packages get imported, then don't automatically
import them in __init__.py. If you want them to be automatically imported,
like os and os.path, then automatically import them. If you don't, don't.
But note that you cannot expect to import kitchen.pot without importing
> One package in question is a huge django application.
> Within the package is one module defining some constants and tiny
> functions, which I would like to reuse from some command line tools,
> which should start quickly without loading any of the django specific
> modules. (the startup penalty and the memory overhead would be noticable)
> I am using rpc4django, which expects, that __init__.py of the django
> application exports some all rpc functions
> which will basically import 95% of the django application and the entire
> django frame work (none of which were required by my command tool,
> support utility for this application)
> I could of course create a separate package just for this tiny sub
> module, but somehow it doesn't feel right to me.
Why should it be a package? Just make it a stand-alone module.
Or use path manipulation from your command line tool to import it on its
own. It might be somewhat of a hack, but if need to do it, do so.
Or do something like this:
+-- __init__.py # lightweight, nearly empty
+-- __init__.py # huge, imports my_app.module1, my_app.module2, etc.
then point rpc4django at my_app.rpc instead of my_app.
More information about the Python-list