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

B

Bill Davy

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:

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)

bs.GotoLimit(1)

bs.Stop()

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,

Bill

PS I posted this to (e-mail address removed) but it did not seem to get through.

00000000000000000000000000000000000000000000000000000000

SHIP.i:

%module SHIP

%{

#include "SHIPIF.hpp"

%}

%include "SHIPIF.hpp"

00000000000000000000000000000000000000000000000000000000

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

return

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

else:

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

try:

_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):

try:

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)

_SHIP.ObjectCounterC_swigregister(ObjectCounterCPtr)

class CallBack(_object):

__swig_setmethods__ = {}

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

__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):

try:

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)

_SHIP.CallBack_swigregister(CallBackPtr)

class BallScrew(_object):

__swig_setmethods__ = {}

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

__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):

try:

if self.thisown: destroy(self)

except: pass

def SetNonExposeCurrent(*args): return
_SHIP.BallScrew_SetNonExposeCurrent(*args)

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)

_SHIP.BallScrew_swigregister(BallScrewPtr)

BallScrewCalBackFunction = _SHIP.BallScrewCalBackFunction
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top