Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jamesapp

macrumors 6502a
Original poster
Mar 7, 2008
544
0
working on a web tutorial. In the first line of the script from the tutorial, there is an import of CoreGraphics. The script it is supposed to draw a yellow rectangle with a red border. My problem is that when i go to run the file by typing:
Code:
python example1.py
i get the following error from terminal:
Code:
james-collinss-macbook-pro:python jamescollins$ python example1.py
Traceback (most recent call last):
  File "example1.py", line 1, in <module>
    from CoreGraphics import *
ImportError: No module named CoreGraphics
i searched for CoreGraphics with spotlight and i found two folders that matched CoreGraphics. One was located in:
Code:
 /Developer/SDKs/MacOS10.4u.sdk/Developer/Headers/CFMCarbon
there is another folder located in:
Code:
/Developer/SDKs/MacOS10.5.sdk/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks
here is the script which i called example1.py. According to the tutorial,
example1.py is taken from, /Developer/Examples/Quartz/Python/ from a script called bitmap.py.

Code:
from CoreGraphics import *
import math

# Create an RGB bitmap context, transparent black background, 256x256

cs = CGColorSpaceCreateDeviceRGB ()
c = CGBitmapContextCreateWithColor (256, 256, cs, (0,0,0,0))

# Draw a yellow square with a red outline in the center

c.saveGState ()
c.setRGBStrokeColor (1,0,0,1)			# red
c.setRGBFillColor (1,1,0,1)			# yellow
c.setLineWidth (3)
c.setLineJoin (kCGLineJoinBevel)
c.addRect (CGRectMake (32.5, 32.5, 191, 191))
c.drawPath (kCGPathFillStroke);
c.restoreGState ()

# Write the bitmap to disk in PNG format

c.writeToFile ("out.png", kCGImageFormatPNG)

the script example1.py which i took from bitmap.py is a little different than the tutorials. Just wondering about what i would do to make this script work.
 

antibact1

macrumors 6502
Jun 1, 2006
334
0
I copied bitmap.py to my home directory and was able to run it successfully with:

python bitmap.py

Not sure what is going on that you can't. The only thing I can think of is you installed a separate copy of python that isn't aware of the CoreGraphics module.

On my system, I also have CoreGraphics related python files in the following locations:

/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/plat-mac/Carbon/CoreGraphics.py
/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/plat-mac/Carbon/CoreGraphics.pyc
/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/plat-mac/Carbon/CoreGraphics.pyo
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/CoreGraphics
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/CoreGraphics/_CoreGraphics.so
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/CoreGraphics/__init__.py
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/CoreGraphics/__init__.pyc
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/CoreGraphics/__init__.pyo
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/PyObjC/Quartz/CoreGraphics
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/PyObjC/Quartz/CoreGraphics/PyObjCOverrides.bridgesupport
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/PyObjC/Quartz/CoreGraphics/__init__.py
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/PyObjC/Quartz/CoreGraphics/__init__.pyc
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/PyObjC/Quartz/CoreGraphics/_callbacks.so
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/PyObjC/Quartz/CoreGraphics/_contextmanager.py
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/PyObjC/Quartz/CoreGraphics/_contextmanager.pyc
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/PyObjC/Quartz/CoreGraphics/_doubleindirect.so
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/PyObjC/Quartz/CoreGraphics/_inlines.so
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/PyObjC/Quartz/CoreGraphics/_sortandmap.so
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac/Carbon/CoreGraphics.py
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac/Carbon/CoreGraphics.pyc
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac/Carbon/CoreGraphics.pyo
 

jamesapp

macrumors 6502a
Original poster
Mar 7, 2008
544
0
i have a related question:
when i open terminal and type
Code:
python example1.py
Code:
james-collinss-macbook-pro:~ jamescollins$ python example1.py
/Library/Frameworks/Python.framework/Versions/2.5/Resources/Python.app/Contents/MacOS/Python: can't open file 'example1.py': [Errno 2] No such file or directory
wondering why my computer can't open a file which i placed in the following location:
my HOME directory in a folder i called python?
i did install a seperate copy of python.
 

antibact1

macrumors 6502
Jun 1, 2006
334
0
i have a related question:
when i open terminal and type
Code:
python example1.py
Code:
james-collinss-macbook-pro:~ jamescollins$ python example1.py
/Library/Frameworks/Python.framework/Versions/2.5/Resources/Python.app/Contents/MacOS/Python: can't open file 'example1.py': [Errno 2] No such file or directory
wondering why my computer can't open a file which i placed in the following location:
my HOME directory in a folder i called python?
i did install a seperate copy of python.

About example1.py, you appear to be in your top-level home directory, not the python one where the example is located. Try:

python python/example1.py

or enter the python directory and issue the same command you tried before.

From the error message you received, it seems you are using the system python and not the one you installed, so I'm not sure why you can't see the CoreGraphics module. The easiest way to find this out the one you are using is to issue a:

which python

If you are using the one you installed, it will not have the CoreGraphics package installed and thus will not work with the CoreGraphics example.
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
So "import" searches for modules on a particular search path.

You can do something like this to check the default path for your python interpreter:

Code:
python -c 'import sys;print sys.path'

On my system -- Leopard with vanilla Apple python -- CoreGraphics can be found in System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/CoreGraphics/

So I need System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python in my sys.path. The Apple installation of Python has that in the default path already.

One easy way to add directories to your default search path if you need to is by setting your PYTHONPATH environment variable.

Hope that helps.
 

jamesapp

macrumors 6502a
Original poster
Mar 7, 2008
544
0
hello, i tried what you said.
i don't have CoreGraphics in the list that i got from terminal when i typed
Code:
python -c 'import sys;print sys.path'
wondering what i did wrong to my version of python, i.e. why CoreGraphics isn't in my default search path?
i am in the process of uninstalling a version of MacPython on my computer.
i thought about reinstalling MacOS X. Is python part of my system or does it come when i install the developer tools? Just wondering if i should reinstall the developer tools, or do an archive and install of leopard?
I have a related question:
How do i uninstall MacPython?
i got this from http://www.python.org:
Code:
What you get after installing is a number of things:
	•	A MacPython-2.3 folder in your Applications folder. In here you find the PythonIDE Integrated Development Environment; PythonLauncher, which handles double-clicking Python scripts from the Finder; and the Package Manager. 
	•	A fairly standard Unix commandline Python interpreter in /usr/local/bin/python, but without the usual /usr/local/lib/python. 
	•	A framework /Library/Frameworks/Python.framework, where all the action really is, but which you usually do not have to be aware of.

To uninstall MacPython you can simply remove these three things.
i think i can do this uninstall, just looking for advice.
thank you for your help!
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
JamesApp,

I think re-installing your whole OS may be a bit drastic. Why don't you just uninstall your other python and use the one that came with the system for now. I'm not sure if it is installed in the base OS or in the developer tools since I always install the dev tools. But I *think* it's actually in the base OS.

And sorry I wasn't clear earlier, you wouldn't see coregraphics in the sys.path. What you would hope to see is the directory where the coregraphics python module and other similar python modules are found. This is a search path and each directory in that path is scanned for modules that you can import.

Before you go too far, just check to see that you have /usr/bin/python on your system. And then check your sys.path using /usr/bin/python.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.