find out whether a module exists (without importing it)

Michael Poeltl michael.poeltl at univie.ac.at
Tue Aug 7 02:43:31 EDT 2012


in my opinion, "without importing it" makes it unnecessarily complicated.
You just want to know it module xyz exists, or better said can be found
(sys.path).

why not try - except[ - else ]

try:
    import mymodule
except ImportError:
    #  NOW YOU KNOW it does not exist
    #+ and you may react properly
??
* Gelonida N <gelonida at gmail.com> [2012-08-06 22:49]:
> Is this possible.
> 
> let's say I'd like to know whether I could import the module
> 'mypackage.mymodule', meaning,
> whther this module is located somewhere in sys.path
> 
> i tried to use
> 
> imp.find_module(), but
> it didn't find any module name containing a '.'
> 
> Am I doing anything wrong?
> 
> Is there another existing implementation, that helps.
> 
> I could do this manually, but this is something I'd just like to do
> if necessary.
> 
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list

-- 
Michael Poeltl
Computational Materials Physics      voice: +43-1-4277-51409
Univ. Wien, Sensengasse 8/12         fax:   +43-1-4277-9514 (or 9513) 
A-1090 Wien, AUSTRIA   cmp.mpi.univie.ac.at 
-------------------------------------------------------------------------------
ubuntu-11.10 | vim-7.3 | python-3.2.2 | mutt-1.5.21 | elinks-0.12
-------------------------------------------------------------------------------



More information about the Python-list mailing list