[Tutor] Finding the version # of a module, and py module problem

Benjamin Kaplan benjamin.kaplan at case.edu
Fri Aug 6 15:28:39 EDT 2010


On Fri, Aug 6, 2010 at 12:14 PM, W. eWatson <wolftracks at invalid.com> wrote:
> I must be missing something. I tried this. (Windows, IDLE, Python 2.5)
> # Try each module
> import sys
> import numpy
> import scipy
> import string
>
> dependencies = "numyp", "scipy"
> for dependency in dependencies:
>    try:
>        __import__(dependency.name)
>    except ImportError:
>        # Uh oh!
>        dependency.installed = None
>    else:
>        # The module loaded OK. Get a handle to it and try to extract
>        # version info.
>        # Many Python modules follow the convention of providing their
>        # version as a string in a __version__ attribute.
>        module = sys.modules[dependency.name]
>
>        # This is what I default to.
>        dependency.installed = "[version unknown]"
>
>        for attribute_name in ("__version__", "__VERSION__", "VERSION",
>                               "version"):
>            if hasattr(module, attribute_name):
>                dependency.installed = getattr(module, attribute_name)
>                break
>
> The result was this.
> Traceback (most recent call last):
>  File "C:/Users/Wayne/Sandia_Meteors/Trajectory_Estimation/dependency_code",
> line 10, in <module>
>    __import__(dependency.name)
> AttributeError: 'str' object has no attribute 'name'
> --

Try reading the code, not just copying and pasting. dependencies isn't
supposed to be a list of strings. It's a list of objects (at least) a
name and an installed attribute.

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



More information about the Python-list mailing list