import packet.module without importing packet.__init__ ?

Gelonida N gelonida at gmail.com
Sun Sep 11 16:17:56 CEST 2011


Hi Steven,

Thanks again for your answer.


On 09/11/2011 06:51 AM, Steven D'Aprano wrote:
> Gelonida N wrote:

> 
> 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.) 

Exactly. Thus my conclusion, that too many imports (just for commodity)
in __init__.py might not be such a good idea if one wants to allow
leaf-only imports.
> 
> 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. 

Agreed. This is however my problem: Up to my understanding rpc4django
insists on putting code into __init__.py

> 
> 
>> 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.

True

> Or do something like this:
> 
> my_app/
> +--  __init__.py  # lightweight, nearly empty
> +--  cmd_tools.py
> +--  module1.py
> +--  module2.py
> +--  rpc/
>      +-- __init__.py  # huge, imports my_app.module1, my_app.module2, etc.
> 
> then point rpc4django at my_app.rpc instead of my_app.
> 

Up to my knowledge rpc4django just imports all __inits__ of all django
applications to find look for functions with a certain decorator.

I'm not sure, whether django allows nested appllications, but this might
be a solution.





More information about the Python-list mailing list