Fail import with SWIG generated imp.load_module ?

Hvidberg, Martin mhv at dmu.dk
Tue Dec 14 03:43:11 EST 2010


I'm trying to load a module GDAL into a Python script.
The loader/binder that is then called, seems to be generated by SWIG, a project with which I'm unfortunately not familiar.
The part of the SWIG generated code that fails on me is as follow:


------ start code snip 1 -------------
# This file was automatically generated by SWIG (http://www.swig.org).
# Version 1.3.39
#
# Do not make changes to this file unless you know what you are doing--modify
# the SWIG interface file instead.


from sys import version_info
if version_info >= (2,6,0):
    def swig_import_helper():
        from os.path import dirname
        import imp
        fp = None
        try:
            fp, pathname, description = imp.find_module('_gdal', [dirname(__file__)])
        except ImportError:
            import _gdal
            return _gdal
        if fp is not None:
            print "fp:",fp # <- My code ...
            print "pn:",pathname # <- My code ...
            print "de:",description # <- My code ...
            try:
                _mod = imp.load_module('_gdal', fp, pathname, description)
            finally:
                fp.close()
                return _mod
    _gdal = swig_import_helper()
    del swig_import_helper
else:
    import _gdal
del version_info

... <shortened by me>
------ end code snip 1 -------------

I had to redo the indents manually in this mail, I hope I didn't make any mistakes.
When I run the script that activates this code, it returns the following:


------ start code snip 2 -------------
fp: <open file 'C:\gdal\bin\gdal\python\osgeo\_gdal.pyd', mode 'rb' at 0x00B35D88>
pn: C:\gdal\bin\gdal\python\osgeo\_gdal.pyd
de: ('.pyd', 'rb', 3)
Traceback (most recent call last):
    File "C:\Martin\Work_Eclipse\Hilfe\src\check_GDAL.py", line 8, in <module>
        import gdal
    File "C:\gdal\bin\gdal\python\osgeo\gdal.py", line 27, in <module>
        _gdal = swig_import_helper()
    File "C:\gdal\bin\gdal\python\osgeo\gdal.py", line 26, in swig_import_helper
        return _mod
UnboundLocalError: local variable '_mod' referenced before assignment
------ end code snip 2 -------------

It appears to me that the objects returned by imp.find_module, and printed out as fp:, pn: and de:, are valid. So why is mp.load_module returning what appears to be some flavour of NULL ?

I'm using Python version 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)]
The GDAL lib is from http://download.osgeo.org/gdal/win32/1.6/gdalwin32exe160.zip


All suggestions appreciated ...


Best Regards
Martin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20101214/7ba6e752/attachment.html>


More information about the Python-list mailing list