I’m an old-fashioned Pascal programmer, new to OOP and Python, so please bear with me…I’ve got a book on Python and I’ve searched here first (although lots of similar threads – not checked every one)…
I’m trying to write a program to include existing modules written by others in my company. According to my Python book, I should be able to import whole modules or just specific classes. The book says that when ‘import’ is used, it actually runs the specified code (not like the INHERIT I’m used to in Pascal).
I have this structure in the module, mod.py, I want to use:
from x.y.z import stuff
class c1(superclass):
def func1(self):
....
def func2(self, db):
....
with self.db as handler:
....
and I’ve got a basic script, test.py, that does just this:
from mod import c1
print "Hello"
when I execute ‘python test.py’, I get the error message:
'with self.db as handler' - invalid syntax
I think I’m missing something fundamental here, so any help much appreciated.
You have modified the error message, but I assume it looks like
This means that your Python version is too old to know the
withstatement. Update to an implementation that supports python 2.6+.In Python 2.5, you can also add a
__future__declaration at the top ofmod.py, like this: