From: MRAB 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.
>
Look for its name in sys.modules, for example:

'foo' in sys.modules
From: Martin P. Hellwig on
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

From: Chris Rebert on
On 3/5/10, Pete Emerson <pemerson(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.

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.rebertia.com
From: Pete Emerson on
On Fri, Mar 5, 2010 at 12:17 PM, Chris Rebert <clp2(a)rebertia.com> wrote:
> On 3/5/10, Pete Emerson <pemerson(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.
>
> 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.rebertia.com
>

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
From: Pete Emerson on
On Mar 5, 12:06 pm, "Martin P. Hellwig" <martin.hell...(a)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
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5
Prev: Slicing [N::-1]
Next: ANN: Wing IDE 3.2.5 Released