Imports visibility in imported modules problem

Patrick Maupin pmaupin at gmail.com
Sun Aug 24 04:54:18 CEST 2008


On Aug 23, 7:27 pm, "Mohamed Yousef" <harrr... at gmail.com> wrote:

> The problem I'm asking about is how can imported modules be aware of
> other imported modules so they don't have to re-import them (avoiding
> importing problems and Consicing code and imports )

You could import sys and look at sys.modules

> why am i doing this in the first place I'm in the process of a medium
> project where imports of modules start to make a jungle and i wanted
> all needed imports to be in a single file (namely __init__.py) and
> then all imports are made once and other modules feel it

This doesn't sound like a good idea.  If A imports a module which is
automatically imported into B's namespace, that sounds like a
maintenance nightmare.

>
> another reason to do this that my project is offering 2 interfaces
> (Console and GUI through Qt) and i needed a general state class (
> whether i'm in Console or GUI mode) to be available for all , for
> determining state and some public functions ,and just injecting
> Imports everywhere seems a bad technique in many ways (debugging ,
> modifying ...etc )

I still don't understand.

> in PHP "Require" would do the trick neatly ... so is there is
> something I'm missing here or the whole technique is bad in which case
> what do you suggest ?

I don't know what to suggest, in that you haven't yet stated anything
that appears to be a problem with how Python works.  If two different
modules import the same third module, there is no big performance
penalty.  The initialization code for the third module is only
executed on the first import, and the cost of having the import
statement find the already imported module is trivial.



More information about the Python-list mailing list