From: Pete Emerson on
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
From: Pete Emerson on
On Mar 5, 11:24 am, Pete Emerson <pemer...(a)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'
From: Steve Holden on
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.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/

From: Steven D'Aprano on
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
From: Steve Holden on
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.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/

 |  Next  |  Last
Pages: 1 2 3 4 5
Prev: Slicing [N::-1]
Next: ANN: Wing IDE 3.2.5 Released