[MMTK] Improving error handling when netCDF.DLL is missing
Peter
mmtk at maubp.freeserve.co.uk
Fri Jun 25 13:45:03 CEST 2004
Hello Konrad,
As per the title, this suggested change should make identifying netCDF
setup errors much easier. This probably only applies to people using
precompiled packages (which means mostly the Windows users).
I have MMTK working a Windows system, and the following example script
loads fine:
import MMTK
import MMTK.Trajectory
import MMTK.Dynamics
(You can also use the argon.py Molecular Dynamics example script...)
But, if I deliberately remove NETCDF.DLL (simulating an installation
error, such as not putting the DLL on the system path, which I actually
did do wrong when first trying to use MMTK), this script triggers an
unhandled exception.
Specifically a read memory access violation in MMTK_trajectory.pyd.
Note that exact error message I see is probably slightly different
because I have Visual Studio installed, so I am given the chance to
debug and thus find out its MMTK_trajectory.pyd causing the problem.
There is no message from MMTK even hinting at netCDF being the problem,
making diagnostics much harder for the new user.
In fact, the original error occurs during the import of MMTK.Trajectory,
and is caught and the variable NetCDF set to None.
This is the relevant code from Trajectory.py follows:
# Report error if the netCDF module is not available.
try:
from Scientific.IO import NetCDF
except ImportError:
NetCDF = None
When MOST of the classes in Trajectory.py are initialised, they call
this function, which will raise an error if netCDF is not available:
def checkNetCDF():
if NetCDF is None:
raise Utility.MMTKError, \
"Trajectories are not available because the \
netCDF module is missing."
It would appear that importing MMTK.Dynamics is doing something without
checking if NetCDF is None. I would suggest changing Trajectory.py as
follows:
# Report error if the netCDF module is not available.
try:
from Scientific.IO import NetCDF
except ImportError:
NetCDF = None
#Expect this to happen (on Windows) when NETCDF.DLL is missing:
#ImportError: DLL load failed: The specified module could not be
#found.
raise Utility.MMTKError, \
"Trajectories are not available because the \
netCDF module is missing."
If this is done, then my example script will stop cleanly at the import
Trajectory statement, with an error message about netCDF.
(If you adopt this change, then the function checkNetCDF is probably not
needed any more)
Best regards,
Peter
P.S.
I don't know how common this error is (misplacing the NETCDF.DLL), but
if using the precompiled installers on Windows, then installing netCDF
is the most complicated step.
I had a look on the mailing list, and the closest example was Lawrence
Kelly's unhandled exception error problem last year, but he was
compiling his own system using mingw32. It sounds like it took him a
while to trace the error back to netCDF:
http://starship.python.net/pipermail/mmtk/2003/000498.html
More information about the mmtk
mailing list