SWIG/IDLE/Python: F5 SHIP.py works but "import SHIP" gives "ImportError: dynamic module does not def

Discussion in 'Python' started by Bill Davy, May 12, 2005.

  1. Bill Davy

    Bill Davy Guest

    I am working with MSVC6 on Windows XP.

    I have created an MSVC project called SHIP

    I have a file SHIP.i with "%module SHIP" as the first line (file is below).

    I run SHIP.i through SWIG 1.3.24 to obtain SHIP_wrap.cpp and SHIP.py; the
    latter contains the line "import _SHIP".

    I compile SHIP_wrap.cpp and a bunch of files into a DLL which I have the
    linker place in _SHIP.pyd and all goes well.

    I start IDLE (version 1.1.1, with Python 2.4.1)

    If I open SHIP.py in an edit window and run it (using F5) it runs and if I
    do >>> _SHIP.__dict__.keys() I get a load of function names which are like
    the ones I'd expect (new_BallScrew for example, where BallScrew is one of my
    classes [and is the interface class to the thing I am really interested in
    which is already called BallScrewC, before you ask]).

    Then I can do things like:

    >>> bs = BallScrew(0)

    >>> bs.Stop()

    >>> del bs

    They work fine. Except of course I really want to put all that stuff in a
    script, say test1.py:

    import SHIP # This is where my problems begin.

    bs = BallScrew(0)



    del bs

    Which I do and when I run test1.py using F5, the line "import SHIP" in
    test1.py produces:

    ImportError: dynamic module does not define init function (initSHIP)

    So, why can I run SHIP.py straight into Python using F5 but not import it
    either directly (>>> import SHIP gives the same error) or indirectly when I
    am running another file?

    One way round this (though I would like to solve this problem as it will
    make things easier as the application grows) is for F5 in IDLE not to
    restart the Python interpreter.

    My aim is for an engineer to be able to use IDLE to write programs to test
    hardware without having to learn C++ to access my OO interface. IDLE and
    Python are ideal but I want to make it easy for them to load the interface
    (i.e. SHIP) between Python and my OO interface.

    Many thanks in advance for any helpful suggestions,


    PS I posted this to but it did not seem to get through.



    %module SHIP


    #include "SHIPIF.hpp"


    %include "SHIPIF.hpp"


    SHIP.py (with no initSHIP L ):

    # This file was created automatically by SWIG.

    # Don't modify this file, modify the SWIG interface instead.

    # This file is compatible with both classic and new-style classes.

    import _SHIP

    def _swig_setattr_nondynamic(self,class_type,name,value,static=1):

    if (name == "this"):

    if isinstance(value, class_type):

    self.__dict__[name] = value.this

    if hasattr(value,"thisown"): self.__dict__["thisown"] = value.thisown

    del value.thisown


    method = class_type.__swig_setmethods__.get(name,None)

    if method: return method(self,value)

    if (not static) or hasattr(self,name) or (name == "thisown"):

    self.__dict__[name] = value


    raise AttributeError("You cannot add attributes to %s" % self)

    def _swig_setattr(self,class_type,name,value):

    return _swig_setattr_nondynamic(self,class_type,name,value,0)

    def _swig_getattr(self,class_type,name):

    method = class_type.__swig_getmethods__.get(name,None)

    if method: return method(self)

    raise AttributeError,name

    import types


    _object = types.ObjectType

    _newclass = 1

    except AttributeError:

    class _object : pass

    _newclass = 0

    del types

    ToString = _SHIP.ToString

    class ObjectCounterC(_object):

    __swig_setmethods__ = {}

    __setattr__ = lambda self, name, value: _swig_setattr(self, ObjectCounterC,
    name, value)

    __swig_getmethods__ = {}

    __getattr__ = lambda self, name: _swig_getattr(self, ObjectCounterC, name)

    def __repr__(self):

    return "<%s.%s; proxy of C++ ObjectCounterC instance at %s>" %
    (self.__class__.__module__, self.__class__.__name__, self.this,)

    def __init__(self, *args):

    _swig_setattr(self, ObjectCounterC, 'this', _SHIP.new_ObjectCounterC(*args))

    _swig_setattr(self, ObjectCounterC, 'thisown', 1)

    def __del__(self, destroy=_SHIP.delete_ObjectCounterC):


    if self.thisown: destroy(self)

    except: pass

    class ObjectCounterCPtr(ObjectCounterC):

    def __init__(self, this):

    _swig_setattr(self, ObjectCounterC, 'this', this)

    if not hasattr(self,"thisown"): _swig_setattr(self, ObjectCounterC,
    'thisown', 0)

    _swig_setattr(self, ObjectCounterC,self.__class__,ObjectCounterC)


    class CallBack(_object):

    __swig_setmethods__ = {}

    __setattr__ = lambda self, name, value: _swig_setattr(self, CallBack, name,

    __swig_getmethods__ = {}

    __getattr__ = lambda self, name: _swig_getattr(self, CallBack, name)

    def __repr__(self):

    return "<%s.%s; proxy of C++ CallBack instance at %s>" %
    (self.__class__.__module__, self.__class__.__name__, self.this,)

    def __init__(self, *args):

    _swig_setattr(self, CallBack, 'this', _SHIP.new_CallBack(*args))

    _swig_setattr(self, CallBack, 'thisown', 1)

    def __del__(self, destroy=_SHIP.delete_CallBack):


    if self.thisown: destroy(self)

    except: pass

    def SetResult(*args): return _SHIP.CallBack_SetResult(*args)

    def GetResult(*args): return _SHIP.CallBack_GetResult(*args)

    def Reset(*args): return _SHIP.CallBack_Reset(*args)

    def GetReady(*args): return _SHIP.CallBack_GetReady(*args)

    class CallBackPtr(CallBack):

    def __init__(self, this):

    _swig_setattr(self, CallBack, 'this', this)

    if not hasattr(self,"thisown"): _swig_setattr(self, CallBack, 'thisown', 0)

    _swig_setattr(self, CallBack,self.__class__,CallBack)


    class BallScrew(_object):

    __swig_setmethods__ = {}

    __setattr__ = lambda self, name, value: _swig_setattr(self, BallScrew, name,

    __swig_getmethods__ = {}

    __getattr__ = lambda self, name: _swig_getattr(self, BallScrew, name)

    def __repr__(self):

    return "<%s.%s; proxy of C++ BallScrew instance at %s>" %
    (self.__class__.__module__, self.__class__.__name__, self.this,)

    def __init__(self, *args):

    _swig_setattr(self, BallScrew, 'this', _SHIP.new_BallScrew(*args))

    _swig_setattr(self, BallScrew, 'thisown', 1)

    def Stop(*args): return _SHIP.BallScrew_Stop(*args)

    def __del__(self, destroy=_SHIP.delete_BallScrew):


    if self.thisown: destroy(self)

    except: pass

    def SetNonExposeCurrent(*args): return

    def Move(*args): return _SHIP.BallScrew_Move(*args)

    class BallScrewPtr(BallScrew):

    def __init__(self, this):

    _swig_setattr(self, BallScrew, 'this', this)

    if not hasattr(self,"thisown"): _swig_setattr(self, BallScrew, 'thisown', 0)

    _swig_setattr(self, BallScrew,self.__class__,BallScrew)


    BallScrewCalBackFunction = _SHIP.BallScrewCalBackFunction
    Bill Davy, May 12, 2005
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Hodapp
    Chris Hodapp
    Dec 17, 2003
  2. Christian Seberino
    Christian Seberino
    Feb 12, 2004
  3. Balaji
    Michael Hudson
    Aug 12, 2004
  4. Bill Davy
    Jun 24, 2009
  5. Bill Davy
    Jul 27, 2011

Share This Page