Python syntax error from golly

For scripts to aid with computation or simulation in cellular automata.
Post Reply
Rich Holmes
Posts: 55
Joined: October 31st, 2015, 1:13 am

Python syntax error from golly

Post by Rich Holmes » September 8th, 2016, 8:16 am

I'm using golly 2.8 on a Mac runnning OS X 10.11.6 and Python 2.7.11.

Tracking down a problem with dvgrn's isotropic-rule-gen.py led me to create foo.py containing only these two lines:

Code: Select all

a = {"boy":"dog"}
rule = { k: False for k, v in a.iteritems() }
If I run this from the OS X command line it executes, and if I look at it interactively it gives the expected result:

Code: Select all

Python 2.7.11 (default, May 15 2016, 08:21:34) 
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a = {"boy":"dog"}
>>> rule = { k: False for k, v in a.iteritems() }
>>> rule
{'boy': False}
but if I run foo.py from golly it throws a syntax error:
Screen Shot 2016-09-08 at 8.04.22 AM.png
Screen Shot 2016-09-08 at 8.04.22 AM.png (32.29 KiB) Viewed 5155 times
If I do the same from golly on my Linux computer it has no complaints. And on my Mac, this works:

Code: Select all

a = {"boy":"dog"}
rule = {}
for k, v in a.iteritems():
	rule[k] = False
Any ideas?

User avatar
dvgrn
Moderator
Posts: 10612
Joined: May 17th, 2009, 11:00 pm
Location: Madison, WI
Contact:

Re: Python syntax error from golly

Post by dvgrn » September 8th, 2016, 1:29 pm

Tom Rokicki wrote on golly-test:
Tom Rokicki wrote:Sounds like he is not picking up the Python he thinks he is.
I suspect he's picking up an old Python 2.6 somehow.
It would have been smart of me to think of that... Might be worth checking your GollyPrefs file to see what version of Python it's pointing to, and/or run

Code: Select all

import sys
import golly as g
g.note(sys.version)
and make sure you really get "2.7.11" back.

Rich Holmes
Posts: 55
Joined: October 31st, 2015, 1:13 am

Re: Python syntax error from golly

Post by Rich Holmes » September 8th, 2016, 2:11 pm

That indeed is it. I see nothing in GollyPrefs about Python version, but sys.version does indeed say 2.6.x...

How does one tell Golly what version to use?

User avatar
dvgrn
Moderator
Posts: 10612
Joined: May 17th, 2009, 11:00 pm
Location: Madison, WI
Contact:

Re: Python syntax error from golly

Post by dvgrn » September 8th, 2016, 2:44 pm

Rich Holmes wrote:That indeed is it. I see nothing in GollyPrefs about Python version, but sys.version does indeed say 2.6.x...

How does one tell Golly what version to use?
I had forgotten about this section at the bottom of the Golly's "Python Scripting" help file:
Python on Mac OS X

Even though Apple supply Python with OS X, it is usually not the latest version. Golly is linked against the Python 2.6 framework stored in /System/Library/Frameworks. If you have Python 2.7 installed in your system you can tell Golly to use it by quitting Golly, opening Terminal.app and entering this command (on one line):

Code: Select all

install_name_tool -change /System/Library/Frameworks/Python.framework/Versions/2.6/Python 
/Library/Frameworks/Python.framework/Versions/2.7/Python 
/path/to/Golly/folder/Golly.app/Contents/MacOS/Golly
If you ever need to switch back to the older Python then just run the command again with the framework paths swapped. To verify which version of Python Golly is using, copy the following code and select Run Clipboard from the File menu:

Code: Select all

import golly, sys 
golly.note(sys.version)
On Windows, just for the record, usually Golly can figure things out with no user input. The location is recorded in GollyPrefs in a line like this:

python_lib=python27.dll

No specific path is needed as long as python27.dll can be found somewhere in the system path. But if you close Golly, edit the above python_lib line in GollyPrefs, and re-run Golly, that will switch to a different Python dll -- or if you simply delete the line and re-run, you get an opportunity to browse to find the DLL you want.

User avatar
Andrew
Moderator
Posts: 919
Joined: June 2nd, 2009, 2:08 am
Location: Melbourne, Australia
Contact:

Re: Python syntax error from golly

Post by Andrew » September 8th, 2016, 7:39 pm

Rich Holmes wrote:I see nothing in GollyPrefs about Python version, but sys.version does indeed say 2.6.x...
Just to clarify, the Mac version of Golly is linked against Python 2.6 so we can distribute a single build that runs on all Macs from 10.6 up. (Although given that very few people are still using 10.6, and that it's possible to install Python 2.7 on 10.6, I think in future I'll just link against Python 2.7.)

The reason there's no mention of a Python version in GollyPrefs is that on the Mac the Python lib is loaded dynamically at start-up time (using a framework path stored in the binary). On Windows and Linux the Python libs are loaded the first time a .py script is executed. On those platforms the python_lib path in GollyPrefs tells Golly which file to load (a .dll file on Windows, a .so file on Linux).

As Dave mentioned above, the solution to Rich's problem is to use the install_name_tool command to change the framework path so that Python 2.7 will be loaded at start-up. I've set up a couple of aliases called newpy and oldpy so I can easily toggle between 2.7 and 2.6:

Code: Select all

[akt-iMac:~] akt% alias
...
newpy    install_name_tool -change /System/Library/Frameworks/Python.framework/Versions/2.6/Python /Library/Frameworks/Python.framework/Versions/2.7/Python /HD/Golly/golly/Golly.app/Contents/MacOS/Golly

oldpy    install_name_tool -change /Library/Frameworks/Python.framework/Versions/2.7/Python /System/Library/Frameworks/Python.framework/Versions/2.6/Python /HD/Golly/golly/Golly.app/Contents/MacOS/Golly
Use Glu to explore CA rules on non-periodic tilings: DominoLife and HatLife

Rich Holmes
Posts: 55
Joined: October 31st, 2015, 1:13 am

Re: Python syntax error from golly

Post by Rich Holmes » September 8th, 2016, 7:50 pm

Yes, that fixes the problem. Thanks!

Post Reply