How to run a Python script.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Mullin
    New Member
    • Sep 2010
    • 2

    How to run a Python script.

    I am running Mac OS 10.6.4...I know this is embarrassingly simple, but I've spent hours on this. I'm beginning to think there might be an installation problem with Python. I stupidly removed MacPython from my Applications file, then when I realized what I had done, downloaded and installed Python2.6.6
    Using IDLE, I created test.py. File attached.
    In a terminal window, after chmod +x test.py and then ./test.py, I get this error: syntax error near unexpected token `('
    Here is the complete contents of the terminal window:

    Last login: Tue Sep 21 12:49:02 on ttys000
    mjmmacbook:~ michaelmullin$ chmod +x test.py
    mjmmacbook:~ michaelmullin$ ./test.py
    ./test.py: line 1: syntax error near unexpected token `('
    ./test.py: line 1: `Python 2.6.6 (r266:84374, Aug 31 2010, 11:00:51) '
    mjmmacbook:~ michaelmullin$


    I get the same error even when the .py file contains a real script (downloaded from web and tested by many)
    Including #!/usr/bin/env python2.6.6 (or just ...python) does not change the error.

    I've been thinking about reloading my operating system, but not sure that will help and its a little radical.

    I hope someone can get me on track.
    Many Thanks,
    Michael
    Attached Files
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    ./test.py: line 1: syntax error near unexpected token `('
    ./test.py: line 1: `Python 2.6.6 (r266:84374, Aug 31 2010, 11:00:51) '
    You have
    (Python 2.6.6 (r266:84374, Aug 31 2010, 11:00:51)
    on the first line of the program, which the interpreter doesn't like. Usually that happens when you think you are running one program, but are actually running another program, perhaps in another directory.

    Comment

    • Michael Mullin
      New Member
      • Sep 2010
      • 2

      #3
      Thanks

      Thanks dwbias, I see what you are saying.
      I now realize I can run the script without all of the header info, e.g.
      Code:
      #!/usr/bin/env python
      
      from mapnik import *
      
      mapfile = 'world_styles.xml'
      map_output = 'hello_world_using_xml_config.jpg'
      projection = '+proj=latlong +datum=WGS84'
      
      m = Map(600,400)
      load_map(m, mapfile)
      bbox = Envelope(Coord(-180, 90), Coord(180.0, -90))
      m.zoom_to_box(bbox)
      render_to_file(m, map_output)
      This is similar to the code I was trying to run in the first place.
      Last edited by bvdet; Sep 22 '10, 11:28 PM. Reason: Please use code tags when posting code. [code] ....code goes here.... [/code]

      Comment

      Working...