You are not logged in.
So I have a script in a file called /home/ricky/run.py. I wrote it using gedit which has the terminal and python console plugins. I have used an internet based python console to run the code and the code worked just fine. However, when I run $ python /home/ricky/run.py in the terminal I get an error:
File "/home/ricky/run.py", line 3
print 'X'
^
SyntaxError: invalid syntax
Here is the actual working script:
class X(object):
def test(self):
print 'X'
class Y(object):
def test(self):
print 'Y'
#class XY(X,Y):
# def test(self):
# super(X,self).test()
# super(Y,self).test() # AttributeError: 'super' object has no attribute 'test'
class XY(X,Y):
def test(self):
X.test(self)
Y.test(self)
class Z(XY):
def test(self):
super(XY,self).test()
X().test() # prints "X"
Y().test() # prints "Y"
XY().test() # prints "X\nY"
Z().test() # prints "X", why not "X\nY"?
The question is.. why does this code work fine in other applications but several times I have tried to get scripts to run in the terminal or in the python console and it fails to run with similar error messages? I know there must be something I am missing. Any help on this topic would be appreciated.
Last edited by rg_arc (2012-03-31 13:41:01)
Offline
$ ls -l `which python`
lrwxrwxrwx 1 root root 7 21. Nov 18:05 /usr/bin/python -> python3
Arch uses python3 by default. Your web console probably used python2. You could either run your script localy with
$ python2 yourscript.py
Or port it to python3 syntax, e. g.:
print('X')
p. s. There is also a helper script called 2to3 that can help you with porting to the python3 syntax:
$ 2to3 test.py
--- test.py (original)
+++ test.py (refactored)
@@ -1,10 +1,10 @@
class X(object):
def test(self):
- print 'X'
+ print('X')
class Y(object):
def test(self):
- print 'Y'
+ print('Y')
#class XY(X,Y):
# def test(self):
Last edited by Wey (2012-03-31 12:29:08)
Offline
ah! Thanks that was alot of help. I really thought I was going crazy for a second LOL
Offline