LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Python Installed; bash command: python base.py. gets syntax error in base.py (https://www.linuxquestions.org/questions/linux-software-2/python-installed%3B-bash-command-python-base-py-gets-syntax-error-in-base-py-854503/)

cre84j 01-05-2011 10:20 PM

Python Installed; bash command: python base.py. gets syntax error in base.py
 
Python is installed
Apparently it's pyGTK2 ( it's 2.6.5 according to Synaptic Package Manager.

this is what shows on the bash terminal (trying to keep the
main directory uncluttered hence the webdev-pp directoy; it's my
main workspace)
Code:

gregory@gregory-laptop:~/webdev-pp$ cd python-pp5
gregory@gregory-laptop:~/webdev-pp/python-pp5$ ls
base.py  pygtkconsole.py
gregory@gregory-laptop:~/webdev-pp/python-pp5$ python base.py
  File "base.py", line 5
    5  import pygtk
            ^
SyntaxError: invalid syntax
gregory@gregory-laptop:~/webdev-pp/python-pp5$

the base.py script is from a tutorial I am following
this is the script
Code:

1  #!/usr/bin/env python
2
3  # example base.py
4
5  import pygtk
6  pygtk.require('2.0')
7  import gtk
8
9  class Base:
10      def __init__(self):
11          self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
12          self.window.show()
13
14      def main(self):
15          gtk.main()
16
17  print __name__
18  if __name__ == "__main__":
19      base = Base()
20      base.main()

It seems obvious that python is not in the directory path or something like that. I have done the same sort of thing with ruby
and it works fine but think it must be something to do with the installation and if it is a systems wide installation or not
but am not sure what to try next.

Thank you in advance for any input you care to give. I am very new
to Linux and computer languages since late last year though some experience with PHP and MySQL from earlier last year and some Javascript during Multimedia studies while getting my Graphic design diploma. Its very new to me and just back from a long break on Holiday but what I have learned is coming back to me. I would appreciate your help very much.

pgpython 01-06-2011 03:41 AM

The problem is with the line numbers in the file. You need to get rid of them and then it will work

cre84j 01-07-2011 02:18 AM

thanks I had to take the indents of of the lines as well and now it works

I do appreciate that python seems to need all the spacings to be right before it will
interpret the script so that seems to be a big part of the language.

thanks pgpython

pgpython 01-07-2011 03:47 AM

That is correct, indentation is how python delimits blocks instead of braces. I don't mind this to be honest as good code should have good indentation anyway. The only thing you have to be careful of is if you mix tabs and spaces. The way to avoid this common problem is to set your editor up correctly. use a standard of 4 spaces of tabs and you should be fine

archtoad6 01-23-2011 04:46 PM

cre84j, Solved? Please mark it so. TIA.


All times are GMT -5. The time now is 06:15 AM.