By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 1,932 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

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

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

May 21 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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
May 21 '07 #2

P: n/a
js
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 <op*****@ukr.netwrote:
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
May 21 '07 #3

P: n/a
On May 21, 11:17 pm, dmitrey <open...@ukr.netwrote:
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?

May 22 '07 #4

P: n/a
On May 21, 11:17 pm, dmitrey <open...@ukr.netwrote:
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?!

May 22 '07 #5

P: n/a
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
May 22 '07 #6

P: n/a
Asun Friere <af*****@yahoo.co.ukwrites:
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.
May 22 '07 #7

P: n/a
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.invalidwrote:
Asun Friere <af*****@yahoo.co.ukwrites:
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
May 23 '07 #8

P: n/a
kaens <ap***************@gmail.comwrites:
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.
May 23 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.