[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Conditional based on whether or not a module is being used

Pete Emerson

3/5/2010 7:25:00 PM

In a module, how do I create a conditional that will do something
based on whether or not another module has been loaded?

Suppose I have the following:

import foo
import foobar

print foo()
print foobar()

########### foo.py
def foo:
return 'foo'

########### foobar.py
def foobar:
if foo.has_been_loaded(): # This is not right!
return foo() + 'bar' # This might need to be foo.foo() ?
else:
return 'bar'

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
21 Answers

Pete Emerson

3/5/2010 7:40:00 PM

0

On Mar 5, 11:24 am, Pete Emerson <pemer...@gmail.com> wrote:
> In a module, how do I create a conditional that will do something
> based on whether or not another module has been loaded?
>
> Suppose I have the following:
>
> import foo
> import foobar
>
> print foo()
> print foobar()
>
> ########### foo.py
> def foo:
>     return 'foo'
>
> ########### foobar.py
> def foobar:
>     if foo.has_been_loaded(): # This is not right!
>         return foo() + 'bar'      # This might need to be foo.foo() ?
>     else:
>         return 'bar'
>
> 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

Aha, progress. Comments appreciated. Perhaps there's a different and
more conventional way of doing it than this?

def foobar():
import sys
if 'foomodule' in sys.modules.keys():
import foomodule
return foomodule.foo() + 'bar'
else:
return 'bar'

Steve Holden

3/5/2010 7:52:00 PM

0

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?
>
> Suppose I have the following:
>
> import foo
> import foobar
>
> print foo()
> print foobar()
>
> ########### foo.py
> def foo:
> return 'foo'
>
> ########### foobar.py
> def foobar:
> if foo.has_been_loaded(): # This is not right!
> return foo() + 'bar' # This might need to be foo.foo() ?
> else:
> return 'bar'
>
> 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

One way would be

if "foo" in sys.modules:
# foo was imported

However that won't get you all the way, since sys.modules["foo"] will be
set even if the importing statement was

from foo import this, that, the_other

So you might want to add

foo = sys.modules["foo"]

inside the function.

regards
Steve

--
Steve Holden +1 571 484 6266 +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010 http://us....
Holden Web LLC http://www.hold...
UPCOMING EVENTS: http://holdenweb.event...

Steve Holden

3/5/2010 7:53:00 PM

0

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?
>
> Suppose I have the following:
>
> import foo
> import foobar
>
> print foo()
> print foobar()
>
By the way, the above statements are never going to work, because
modules aren't callable. Maybe you want

print foo.foo()
print foobar.foobar()

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010 http://us....
Holden Web LLC http://www.hold...
UPCOMING EVENTS: http://holdenweb.event...

Steven D'Aprano

3/5/2010 7:54:00 PM

0

On Fri, 05 Mar 2010 11:24:44 -0800, 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?

try:
import foo
except ImportError:
foo = None

def function():
if foo:
return foo.func()
else:
do_something_else()



Or, alternatively:

try:
import foo
except ImportError:
import alternative_foo as foo # This better succeed!

def function():
return foo.func()


--
Steven

MRAB

3/5/2010 7:58:00 PM

0

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?
>
> Suppose I have the following:
>
> import foo
> import foobar
>
> print foo()
> print foobar()
>
> ########### foo.py
> def foo:
> return 'foo'
>
> ########### foobar.py
> def foobar:
> if foo.has_been_loaded(): # This is not right!
> return foo() + 'bar' # This might need to be foo.foo() ?
> else:
> return 'bar'
>
> 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.
>
Look for its name in sys.modules, for example:

'foo' in sys.modules

Martin P. Hellwig

3/5/2010 8:06:00 PM

0

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

Chris Rebert

3/5/2010 8:18:00 PM

0

On 3/5/10, Pete Emerson <pemerson@gmail.com> wrote:
> In a module, how do I create a conditional that will do something
> based on whether or not another module has been loaded?
>
> Suppose I have the following:
>
> import foo
> import foobar
>
> print foo()
> print foobar()
>
> ########### foo.py
> def foo:
> return 'foo'
>
> ########### foobar.py
> def foobar:
> if foo.has_been_loaded(): # This is not right!
> return foo() + 'bar' # This might need to be foo.foo() ?
> else:
> return 'bar'
>
> 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.

Just try importing foo, and then catch the exception if it's not installed.

#foobar.py
try:
import foo
except ImportError:
FOO_PRESENT = False
else:
FOO_PRESENT = True

if FOO_PRESENT:
def foobar():
return foo.foo() + 'bar'
else:
def foobar():
return 'bar'


You could alternately do the `if FOO_PRESENT` check inside the
function body rather than defining separate versions of the function.

Cheers,
Chris
--
http://blog.re...

Pete Emerson

3/5/2010 8:26:00 PM

0

On Fri, Mar 5, 2010 at 12:17 PM, Chris Rebert <clp2@rebertia.com> wrote:
> On 3/5/10, Pete Emerson <pemerson@gmail.com> wrote:
>> In a module, how do I create a conditional that will do something
>> based on whether or not another module has been loaded?
>>
>> Suppose I have the following:
>>
>> import foo
>> import foobar
>>
>> print foo()
>> print foobar()
>>
>> ########### foo.py
>> def foo:
>>    return 'foo'
>>
>> ########### foobar.py
>> def foobar:
>>    if foo.has_been_loaded(): # This is not right!
>>        return foo() + 'bar'      # This might need to be foo.foo() ?
>>    else:
>>        return 'bar'
>>
>> 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.
>
> Just try importing foo, and then catch the exception if it's not installed.
>
> #foobar.py
> try:
>    import foo
> except ImportError:
>    FOO_PRESENT = False
> else:
>    FOO_PRESENT = True
>
> if FOO_PRESENT:
>    def foobar():
>        return foo.foo() + 'bar'
> else:
>    def foobar():
>        return 'bar'
>
>
> You could alternately do the `if FOO_PRESENT` check inside the
> function body rather than defining separate versions of the function.
>
> Cheers,
> Chris
> --
> http://blog.re...
>

Except I want to use the module only if the main program is using it
too, not just if it's available for use. I think that I found a way in
my follow-up post to my own message, but not sure it's the best way or
conventional.

Pete

Pete Emerson

3/5/2010 8:31:00 PM

0

On Mar 5, 12:06 pm, "Martin P. Hellwig" <martin.hell...@dcuktec.org>
wrote:
> 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.

Pete

Pete Emerson

3/5/2010 8:32:00 PM

0

On Mar 5, 11:57 am, MRAB <pyt...@mrabarnett.plus.com> wrote:
> 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?
>
> > Suppose I have the following:
>
> > import foo
> > import foobar
>
> > print foo()
> > print foobar()
>
> > ########### foo.py
> > def foo:
> >     return 'foo'
>
> > ########### foobar.py
> > def foobar:
> >     if foo.has_been_loaded(): # This is not right!
> >         return foo() + 'bar'      # This might need to be foo.foo() ?
> >     else:
> >         return 'bar'
>
> > 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.
>
> Look for its name in sys.modules, for example:
>
>      'foo' in sys.modules

Excellent, this is what I finally discovered, although I was looking
for 'foo' in sys.modules.keys(), which apparently isn't necessary.