Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

howto check does module 'asdf' exist? (is available for import)

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


openopt at ukr

May 21, 2007, 6:17 AM

Post #1 of 10 (99 views)
Permalink
howto check does module 'asdf' exist? (is available for import)

howto check does module 'asdf' exist (is available for import) or no?
(without try/cache of course)
Thx in advance, D.

--
http://mail.python.org/mailman/listinfo/python-list


stefan.behnel-n05pAM at web

May 21, 2007, 6:36 AM

Post #2 of 10 (95 views)
Permalink
Re: howto check does module 'asdf' exist? (is available for import) [In reply to]

dmitrey wrote:
> howto check does module 'asdf' exist (is available for import) or no?

Walk through sys.path and find it yourself?


> (without try/cache of course)

Why is the obvious (and most common) try/import/catch solution "of course" out?

Stefan
--
http://mail.python.org/mailman/listinfo/python-list


exarkun at divmod

May 21, 2007, 6:39 AM

Post #3 of 10 (95 views)
Permalink
Re: howto check does module 'asdf' exist? (is available for import) [In reply to]

On 21 May 2007 06:17:16 -0700, dmitrey <openopt [at] ukr> wrote:
>howto check does module 'asdf' exist (is available for import) or no?
>(without try/cache of course)
>Thx in advance, D.
>

You could use twisted.python.modules:

$ python
Python 2.4.3 (#2, Oct 6 2006, 07:52:30)
[GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from twisted.python.modules import getModule
>>> from sys import modules
>>> 'Numeric' in modules
False
>>> getModule('Numeric')
PythonModule<'Numeric'>
>>> 'Numeric' in modules
False
>>>

Jean-Paul
--
http://mail.python.org/mailman/listinfo/python-list


ebgssth at gmail

May 21, 2007, 6:57 AM

Post #4 of 10 (94 views)
Permalink
Re: howto check does module 'asdf' exist? (is available for import) [In reply to]

Why not just use try?
Trying to import a module is a python idiom.
http://www.diveintopython.org/file_handling/index.html

On 21 May 2007 06:17:16 -0700, dmitrey <openopt [at] ukr> wrote:
> howto check does module 'asdf' exist (is available for import) or no?
> (without try/cache of course)
> Thx in advance, D.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


afriere at yahoo

May 21, 2007, 11:42 PM

Post #5 of 10 (88 views)
Permalink
Re: howto check does module 'asdf' exist? (is available for import) [In reply to]

On May 21, 11:17 pm, dmitrey <open...@ukr.net> wrote:
> howto check does module 'asdf' exist (is available for import) or no?

try :
import asdf
del asdf
except ImportError :
#do stuff ...

> (without try/cache of course)

Oops sorry, you didn't want it the obvious way ... but why ever not?



--
http://mail.python.org/mailman/listinfo/python-list


afriere at yahoo

May 22, 2007, 12:27 AM

Post #6 of 10 (88 views)
Permalink
Re: howto check does module 'asdf' exist? (is available for import) [In reply to]

On May 21, 11:17 pm, dmitrey <open...@ukr.net> wrote:
> howto check does module 'asdf' exist (is available for import) or no?
try :
import asdf
del asdf
except ImportError :
print "module asdf not available"
else :
print "module asdf available for loading"

You can generalise this, but at the expense of a couple of exec
statements:
def is_module_available (module) :
try :
exec('import %s' % module)
exec('del %s' % module)
except ImportError :
return False
else :
return True

> (without try/cache of course)
Oops sorry, you wanted it done in some non-obvious way! Why?!

--
http://mail.python.org/mailman/listinfo/python-list


carsten at uniqsys

May 22, 2007, 5:29 AM

Post #7 of 10 (85 views)
Permalink
Re: howto check does module 'asdf' exist? (is available for import) [In reply to]

On Tue, 2007-05-22 at 00:27 -0700, Asun Friere wrote:
> You can generalise this, but at the expense of a couple of exec
> statements:
> def is_module_available (module) :
> try :
> exec('import %s' % module)
> exec('del %s' % module)
> except ImportError :
> return False
> else :
> return True

You can save the exec statement by using the built-in __import__
function:

def is_module_available(modulename):
try: mod = __import__(modulename)
except ImportError: return False
else: return True

--
Carsten Haese
http://informixdb.sourceforge.net


--
http://mail.python.org/mailman/listinfo/python-list


"http://phr.cx" at NOSPAM

May 22, 2007, 9:09 AM

Post #8 of 10 (83 views)
Permalink
Re: howto check does module 'asdf' exist? (is available for import) [In reply to]

Asun Friere <afriere [at] yahoo> writes:
> > howto check does module 'asdf' exist (is available for import) or no?
> try :
> import asdf
> del asdf
> except ImportError :
> print "module asdf not available"
> else :
> print "module asdf available for loading"

But this has a side effect: if asdf is already loaded, it deletes it.
--
http://mail.python.org/mailman/listinfo/python-list


apatheticagnostic at gmail

May 22, 2007, 8:10 PM

Post #9 of 10 (80 views)
Permalink
Re: howto check does module 'asdf' exist? (is available for import) [In reply to]

I think that's there because you just wanted to check if it was
available for import, implying that you didn't actually want to import
it right then.

On 22 May 2007 09:09:02 -0700, Paul Rubin
<"http://phr.cx"@nospam.invalid> wrote:
> Asun Friere <afriere [at] yahoo> writes:
> > > howto check does module 'asdf' exist (is available for import) or no?
> > try :
> > import asdf
> > del asdf
> > except ImportError :
> > print "module asdf not available"
> > else :
> > print "module asdf available for loading"
>
> But this has a side effect: if asdf is already loaded, it deletes it.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


"http://phr.cx" at NOSPAM

May 22, 2007, 8:17 PM

Post #10 of 10 (78 views)
Permalink
Re: howto check does module 'asdf' exist? (is available for import) [In reply to]

kaens <apatheticagnostic [at] gmail> writes:
> I think that's there because you just wanted to check if it was
> available for import, implying that you didn't actually want to import
> it right then.

Whether it's available for import and whether you've already imported
it are two separate questions. Maybe you said "import fghj as asdf"
and now you want to find out if there's another module actually named
asdf. It would be cleaner if there's a method with no side effects.
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.