You are not logged in.

#1 2012-03-31 12:13:21

rg_arc
Member
Registered: 2011-03-20
Posts: 507

Question about Python(using the terminal or Gedit)[SOLVED]

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

#2 2012-03-31 12:23:31

Wey
Member
Registered: 2011-04-22
Posts: 217

Re: Question about Python(using the terminal or Gedit)[SOLVED]

$ 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

#3 2012-03-31 13:39:12

rg_arc
Member
Registered: 2011-03-20
Posts: 507

Re: Question about Python(using the terminal or Gedit)[SOLVED]

ah! Thanks that was alot of help. I really thought I was going crazy for a second LOL

Offline

Board footer

Powered by FluxBB