On Aug 6, 2010, at 3:14 PM, W. eWatson wrote:

I must be missing something. I tried this. (Windows, IDLE, Python 2.5)

Yes, as Benjamin Kaplan pointed out and as I said in the email where I posted this code snippet, "dependencies is a list of custom classes that represent modules we need (e.g. numpy)." The code I posted was not meant to be a complete working example. It's part of a larger piece of code that I didn't have time to cook down to a simpler, self- sufficient whole.

Also, in your list you've got "numyp" instead of "numpy".

Also, at the top of your code you have "import numpy" and "import scipy" which defeats the purpose of this code.

Try this (untested):
import sys

dependencies = ("numpy", "scipy", "some_other_module")
for dependency in dependencies:
   try:
       __import__(dependency)
   except ImportError:
       # Uh oh!
       print "%s is not installed" % dependency
   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]

       for attribute_name in ("__version__", "__VERSION__", "VERSION",
                              "version"):
           if hasattr(module, attribute_name):
               version = getattr(module, attribute_name)
               print "module %s has version %s" % (dependency, version)
               break



bye
Philip




# 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'
--
http://mail.python.org/mailman/listinfo/python-list

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

Reply via email to