vendredi 25 janvier 2008

Executing Python scripts importing the CoreGraphics module

If you get the following error

Traceback (most recent call last):
File "spiraler.py", line 5, in ?
from CoreGraphics import *
ImportError: No module named CoreGraphics

when trying to execute a Python script which imports the CoreGraphics module, e.g. via

from CoreGraphics import *

the problem lies with the fact that the referred CoreGraphics module is only available in the Python distribution that comes installed with OS X in /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac/Carbon/CoreGraphics.py and doesn't come with standard distributions from www.python.org which install symlinks in /usr/local/bin and modify the PATH environment variable to put /usr/local/bin in front.

A way to address the issue consists, at least for scripts that would be executed directly from the terminal (through chmod u+x), in adding the following line to the first line of the script:

#!/usr/bin/python


Initially took me some time to figure out what was the problem so i hope this will allow anybody reading this to save some time by not having to search for a solution.

Aucun commentaire: