Conditional based on whether or not a module is being used

Pete Emerson pemerson at
Fri Mar 5 21:31:16 CET 2010

On Mar 5, 12:06 pm, "Martin P. Hellwig" <martin.hell... at>
> On 03/05/10 19:24, Pete Emerson wrote:
> > In a module, how do I create a conditional that will do something
> > based on whether or not another module has been loaded?
> <cut>>
> > If someone is using foo module, I want to take advantage of its
> > features and use it in foobar, otherwise, I want to do something else.
> > In other words, I don't want to create a dependency of foobar on foo.
> > My failed search for solving this makes me wonder if I'm approaching
> > this all wrong.
> > Thanks in advance,
> > Pete
> Hmm how about the module is available, just not imported yet, I would
> assume that you still would like to use the module then.
> Perhaps playing around with the imp module might get you what you mean
> instead of what you say?
> --
> mph

I can certainly see why one might want to use it if it's available but
not yet imported. In that case I could do a try / exception block. But
in this case, I actually don't want to use the module unless the main
program is doing it too. But you've got me thinking, I need to make
sure that's really the desired behavior.


More information about the Python-list mailing list