From: Philip Semanchuk on

On Aug 6, 2010, at 10:20 AM, Richard D. Moores wrote:

> On Thu, Aug 5, 2010 at 18:47, Philip Semanchuk
> <philip(a)semanchuk.com> wrote:
>>
>> it's just a question of whether or not
>> the module in question exposes any kind of a version attribute.
>> There's no
>> standard, unfortunately. The most popular convention seems to be
>> via an
>> attribute called __version__, but I've also seen __VERSION__,
>> VERSION, and
>> version.
>>
> Here's one more way:
>>>> import gmpy
>>>> gmpy.__version__
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> AttributeError: 'module' object has no attribute '__version__'
>>>>
>>>> gmpy.version()
> '1.12'

That's the nice thing about standards -- there are so many to choose
from! =)

Thanks for pointing that out; I'll update my code.

bye
Philip
From: W. eWatson on
On 8/5/2010 6:47 PM, Philip Semanchuk wrote:
>
> On Aug 5, 2010, at 8:55 PM, W. eWatson wrote:
>
>> It's been awhile since I've used python, and I recall there is a way
>> to find the version number from the IDLE command line prompt. dir,
>> help, __version.__?
>
> Hi Wayne,
> FYI it's got nothing to do with IDLE, it's just a question of whether or
> not the module in question exposes any kind of a version attribute.
> There's no standard, unfortunately. The most popular convention seems to
> be via an attribute called __version__, but I've also seen __VERSION__,
> VERSION, and version.
>
> Here's some code that I wrote that you might find useful. It's from a
> setup.py and it checks a list of modules on which our project depends to
> see if (a) they're installed and (b) if the version installed is
> adequate. In the snippet below, dependencies is a list of custom classes
> that represent modules we need (e.g. numpy).
>
>
> # Try each module
> 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
>
> Hope this helps a little,
> Philip
>
Thanks. I'll look into it.
From: W. eWatson on
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'
From: Benjamin Kaplan on
On Fri, Aug 6, 2010 at 12:14 PM, W. eWatson <wolftracks(a)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
>
From: Tommy Grav on

On Aug 6, 2010, at 3:14 PM, W. eWatson 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'
> --
> http://mail.python.org/mailman/listinfo/python-list

dependencies = "numpy", "scipy"

is a tuple of two strings, when you do your for loop you
first get "numpy" (a string) and it does not have a .name
attribute.

Tommy