[Tutor] List installed python modules

Tim Golden mail at timgolden.me.uk
Mon Jul 21 14:37:15 CEST 2008


Eli Brosh wrote:
> Hello,
> 
> Is there a way to get a list of installed python modules and their 
> versions ?
> i.e. I would like to know if matplotlib is installed on my computer and 
> what version is it.
> And so with other modules.

You've got two slightly different requirements there:

1) List all modules installed

and

2) Is *this* module installed?

The second is easy:

<code>
try:
 import ModuleX
except ImportError:
 print "ModuleX is not installed"
else:
 print "ModuleX is installed"
 print "with version", getattr (ModuleX, __VERSION__, "unknown")
 
</code>

There's no prescribed attribute for version, so you
might have to check a few like that if you didn't
know what to look for.

The first is harder, and in effect impossible in general.
Given a well-known and controlled setup, you could do
various things like interrogate the system's package
database or scan sys.path looking for modules & packages,
but it's all a bit hazy and wouldn't take into account,
say, a library module local to a particular application
which differs from a centrally-installed one for version
reasons.

TJG



More information about the Tutor mailing list