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

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

Post by W. eWatso » Sat, 07 Aug 2010 09:55:30


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.__?

I made the most minimal change to a program, and it works for me, but
not my partner. He gets

Traceback (most recent call last):
File "C:\Documents and
Settings\HP_Administrator.DavesDesktop\Desktop\NC-FireballReport20100729.py",
line 40, in <module>
from scipy import stats as stats # scoreatpercentile
File "C:\Python25\lib\site-packages\scipy\stats\__init__.py", line 7,
in <module>
from stats import *
File "C:\Python25\lib\site-packages\scipy\stats\stats.py", line 191,
in <module>
import scipy.special as special
File "C:\Python25\lib\site-packages\scipy\special\__init__.py", line
22, in <module>
from numpy.testing import NumpyTest
ImportError: cannot import name NumpyTest

Here are the first few lines of code.

import sys, os, glob
import string
from numpy import *
from datetime import datetime, timedelta
import time
from scipy import stats as stats # scoreatpercentile

I'm pretty sure he has the same version of Python, 2.5, but perhaps not
the numpy or scipy modules. I need to find out his version numbers.

--
Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
Obz Site: 3915' 7" N, 1212' 32" W, 2700 feet
 
 
 

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

Post by Steven D'A » Sat, 07 Aug 2010 10:13:11


It's only a convention, but the usual way is to check the __version__
attribute. It works for Numpy:

'1.0.3'



--
Steven

 
 
 

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

Post by MRAB » Sat, 07 Aug 2010 10:23:51


Try:

import numpy
help(numpy.version)

BTW, on Python 2.6 I can see that there's "numpytest" but not
"NumpyTest".
 
 
 

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

Post by W. eWatso » Sat, 07 Aug 2010 10:40:02


It is now written in my Py book. Thanks.
 
 
 

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

Post by W. eWatso » Sat, 07 Aug 2010 10:42:04


I have to stick with 2.5 for comparability with my partner. He's
non-Python but was able to get Python 2.5 working. I think he somehow
bumped ahead to a later version of numpy than I have.
 
 
 

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

Post by Philip Sem » Sat, 07 Aug 2010 10:47:48


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
 
 
 

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

Post by Richard D. » Sat, 07 Aug 2010 23:20:38


Here's one more way:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__version__'
'1.12'

Dick Moores
 
 
 

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

Post by Philip Sem » Sun, 08 Aug 2010 00:09:17


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
 
 
 

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

Post by W. eWatso » Sun, 08 Aug 2010 02:54:04


Thanks. I'll look into it.
 
 
 

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

Post by W. eWatso » Sun, 08 Aug 2010 04:14:53

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'
 
 
 

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

Post by Benjamin K » Sun, 08 Aug 2010 04:28:39


> xcept ImportErr>r:
> >Uh oh!
> ependency.instal>ed = None >> lse:
> The module loaded OK. Get a handle to it and>try to extract
> gt; version info.
> Many Python modules follow the conven>ion of providing their
> version as a string >n a __version__ attribute.
> odule > s>s.modules[dependency.name]
>
> gt; This is what I default to.
> epen>en>y.installed = "[version unknown]"
>
> or attribute_name in (>__version__", "__VERSION__", >VERSION",
> ">ersion"):
> f hasattr(module, attribute_name):
> > epe>de>cy.installed = getattr(>odule, attribute_name)
> gt;reak
>
> The result was this.
> Traceback (most recent call last):
> >le "C:/Users/Wayne/S><nd>a_Meteors/Trajectory_Estimation/>ependency_code",
> line 10, in
> _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 strin>s. It's a list of objects (at least) a
name and an in>talled attribute.
 
 
 

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

Post by Tommy Gra » Sun, 08 Aug 2010 04:31:36


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
 
 
 

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

Post by Philip Sem » Sun, 08 Aug 2010 11:18:13


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
 
 
 

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

Post by W. eWatso » Mon, 09 Aug 2010 01:12:50


Good. It worked. Are there other attributes of a module that can help
identify it further? Release date, ...?