By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,341 Members | 1,395 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,341 IT Pros & Developers. It's quick & easy.

(MAC) CoreGraphics module???

P: n/a
Running OS X 10.4 "Tiger". Various references
by Apple and others say that there exists a
module that gives Quartz bindings, allowing
all sort of graphics things in Python.

Sure enough, after installing Xcode I have
some sample scripts. They all start with

from CoreGraphics import *

(as do all the examples in two books, both
of which say they're talking about Tiger.)

I get an ImportError trying to import CoreGraphics.
I found a CoreGraphics.py in a folder named Carbon;
when I change the script to read

import Carbon
from Carbon.CoreGraphics import *

there's no import error, but all of the Quartz
things are NameErrors. I look at CoreGraphics.py,
it's just about twenty lines long, defining a few
constants with Quartz-sounding names.

What gives? I'm supposed to do something else?
Someone accidentally deleted all the code from
my CoreGraphics.py? (Is there a working
CoreGraphics.py around somewhere? I found a
file somewhere on the net that was the same
as mine except it ended with

from CG import *

Adding that doesn't change anything.)

(Yes, the XCode installation seems to be working
fine.)

???

************************

David C. Ullrich
Nov 2 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
David C. Ullrich wrote:
Running OS X 10.4 "Tiger". Various references
by Apple and others say that there exists a
module that gives Quartz bindings, allowing
all sort of graphics things in Python.

Sure enough, after installing Xcode I have
some sample scripts. They all start with

from CoreGraphics import *

(as do all the examples in two books, both
of which say they're talking about Tiger.)

I get an ImportError trying to import CoreGraphics.
I found a CoreGraphics.py in a folder named Carbon;
when I change the script to read

import Carbon
from Carbon.CoreGraphics import *

there's no import error, but all of the Quartz
things are NameErrors. I look at CoreGraphics.py,
it's just about twenty lines long, defining a few
constants with Quartz-sounding names.
That's different than the one that is referenced. The one those articles
reference is only available in the Python that came with the system in
/System/Library/Frameworks/Python.framework, not one that you might have
installed from www.python.org into /Library/Frameworks/Python.framework. The
module was made by Apple, and they have not released the source code, so it
cannot be made to work with the www.python.org distribution.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Nov 2 '07 #2

P: n/a
On Thu, 01 Nov 2007 19:39:20 -0500, Robert Kern
<ro*********@gmail.comwrote:
>David C. Ullrich wrote:
>[why doesn't CoreGraphics work?]

That's different than the one that is referenced. The one those articles
reference is only available in the Python that came with the system in
/System/Library/Frameworks/Python.framework, not one that you might have
installed from www.python.org into /Library/Frameworks/Python.framework. The
module was made by Apple, and they have not released the source code, so it
cannot be made to work with the www.python.org distribution.
usenet is amazing - your reply appears to have been
posted a half hour before my question! Thanks.

So CoreGraphics is a builtin in Apple-Python,
explaining why I didn't find the relevant
CoreGraphics.py anywhere on the hard drive, eh?

Fine. Now what? Please feel free to bear in mind
that I have very little experience with the Mac
and with Unix - I'm certain that if I knew what
I was doing there I wouldn't need to ask the
questions below, sorry. And sorry about the length
of this post - there's a second issue that maybe
you could explain, that I'd really love to have
an explanation for before modifying things. Anyway:

Since I didn't do anything special to remove it
when I installed the Python-Python, the Apple-Python
should still be there, yes? I'd appreciate any
advice on how to get things set up the way I'd
like. Below when I refer to the MacPython stuff
I mean ApplicationLauncher (or however it's spelled,
the Mac's at the office and I'm at home) and Idle;
the stuff that gives convenient access to Python
scripts in the GUI.

Note that I don't have any particular reason to
want to use the latest version of Python - I was
actually getting along fine with 1.5 until very
recently. I'd be perfectly happy to set things
up so everything used the Apple-Python. (i) If I
just uninstalled the Python-Python (how?) would
everything (Terminal, the MacPython stuff)
automagically find the Apple-Python instead?
(ii) If not, what would I have to do to make
things use the Apple-Python instead? (And if
I have to do something to make that happen,
would doing that still work if I just left
the Python-Python sitting there?)

OR: Is there something special I could do with
CoreGraphics scripts and/or how I invoke them
so that _they_ would use the Apple-Python while
other scripts would just use the Python-Python
as now?

(A last choice would be a setup where most
scripts work as they do now and I somehow
make CoreGraphics work from Terminal but
not in Finder...)

(Hmm, a wild guess: something somewhere is an
alias to Python-Python, and I just change that
(how?) to an alias to Apple-Python?)

I'm a little nervous about making any changes,
because something very mysterious happened when
I was setting things up the way they are now -
since I have no idea what I did to make things
work the way they are now I don't know that
I could set things up the way they are now
again if I had to. If you can explain the
following that will be something (I've been
meaning to ask about the following sometime,
hasn't been important til now when I'm
contemplating changing the setup):

The history: I get a Mac. I discover that it
includes Python, can't figure out how to
execute .py files in Finder. I hear about
MacPython.

I install the "small" MacPython download, that's
supposed to just add ApplicationLauncher(?),
Idle, etc on top of the existing Python.
Works fine _except_ that when I double-click
a .py file in Finder it executes with the
cwd equal to the root directory instead of
the directory where the script is located.

Trying to fix that I install the "full"
MacPython, including Python itself. Doesn't
help, scripts still execute in "/".

A few weeks later I decide to try to fix that.
The plan is to edit whatever.py, the script
that's supposed to run before everything else
allowing customizations (always takes me a
while to find the magic name in the docs -
probably you know the name I mean). The plan
is to extract the directory I want from
sys.argv and then chdir.

And here's the mysterious part: The day I
plan on modifying whatever.py I find the
problem has fixed itself! When I double-
click .py files they execute in the right
directory.

If you asked what I was smoking I wouldn't blame
you. (Not that I really think that's it, but
the _only_ thing I can imagine I did that
could have led to the change was that perhaps
I hadn't yet tried out Idle when things were
executing in the wrong directory, and somehow
the first time I ran Idle it fixed something
for me.)

Anyway. You have any idea what was going on there,
and/or any idea about what to do to fix that
problem if it appears again? Something somewhere
changed - what, and how do I change it manually?
"defaults write what.what.what what what"?
Or what? I'd be much happier understanding what
happened here before making any changes...

Evidently you've read this far - thanks. Long
posts are irritating. Of course so are posts
that don't describe the problem - oh well.
************************

David C. Ullrich
Nov 2 '07 #3

P: n/a

On 2 nov 2007, at 02.10, David C. Ullrich wrote:
Running OS X 10.4 "Tiger". Various references
by Apple and others say that there exists a
module that gives Quartz bindings, allowing
all sort of graphics things in Python.

Sure enough, after installing Xcode I have
some sample scripts. They all start with

from CoreGraphics import *

(as do all the examples in two books, both
of which say they're talking about Tiger.)

I get an ImportError trying to import CoreGraphics.
I found a CoreGraphics.py in a folder named Carbon;
when I change the script to read

import Carbon
from Carbon.CoreGraphics import *

there's no import error, but all of the Quartz
things are NameErrors. I look at CoreGraphics.py,
it's just about twenty lines long, defining a few
constants with Quartz-sounding names.

What gives? I'm supposed to do something else?
Someone accidentally deleted all the code from
my CoreGraphics.py? (Is there a working
CoreGraphics.py around somewhere? I found a
file somewhere on the net that was the same
as mine except it ended with

from CG import *

Adding that doesn't change anything.)

(Yes, the XCode installation seems to be working
fine.)

???

************************

David C. Ullrich
--
http://mail.python.org/mailman/listinfo/python-list
There are Python wrappers for the Cocoa API. These can be used with
binaries from Python.org.
The name of the module is PyObjC, and can be downloaded from
sourceforge.
There are binary builds for Python up to version 2.4.1.
For Python 2.5.1, it is necessary to build it yourself, which I've
found out requires modifying setup.py.
(The problem is that setup.py tries to use two modules that's not
installed by default on Tiger)
------
What is a woman that you forsake her, and the hearth fire and the
home acre,
to go with the old grey Widow Maker. --Kipling, harp song of the
Dane women
Tommy Nordgren
to************@comhem.se

Nov 2 '07 #4

P: n/a
David C. Ullrich wrote:
On Thu, 01 Nov 2007 19:39:20 -0500, Robert Kern
<ro*********@gmail.comwrote:
>David C. Ullrich wrote:
>>[why doesn't CoreGraphics work?]
That's different than the one that is referenced. The one those articles
reference is only available in the Python that came with the system in
/System/Library/Frameworks/Python.framework, not one that you might have
installed from www.python.org into /Library/Frameworks/Python.framework. The
module was made by Apple, and they have not released the source code, so it
cannot be made to work with the www.python.org distribution.

usenet is amazing - your reply appears to have been
posted a half hour before my question! Thanks.

So CoreGraphics is a builtin in Apple-Python,
explaining why I didn't find the relevant
CoreGraphics.py anywhere on the hard drive, eh?
Okay, which version of OS X do you have? In 10.3 and 10.4 it used to be here:
/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/plat-mac/CoreGraphics.py

I notice that in 10.5, it no longer exists, though.
Fine. Now what? Please feel free to bear in mind
that I have very little experience with the Mac
and with Unix - I'm certain that if I knew what
I was doing there I wouldn't need to ask the
questions below, sorry. And sorry about the length
of this post - there's a second issue that maybe
you could explain, that I'd really love to have
an explanation for before modifying things. Anyway:

Since I didn't do anything special to remove it
when I installed the Python-Python, the Apple-Python
should still be there, yes?
Yes, in the location I mentioned.
I'd appreciate any
advice on how to get things set up the way I'd
like. Below when I refer to the MacPython stuff
I mean ApplicationLauncher (or however it's spelled,
the Mac's at the office and I'm at home) and Idle;
the stuff that gives convenient access to Python
scripts in the GUI.

Note that I don't have any particular reason to
want to use the latest version of Python - I was
actually getting along fine with 1.5 until very
recently. I'd be perfectly happy to set things
up so everything used the Apple-Python. (i) If I
just uninstalled the Python-Python (how?) would
everything (Terminal, the MacPython stuff)
automagically find the Apple-Python instead?
(ii) If not, what would I have to do to make
things use the Apple-Python instead? (And if
I have to do something to make that happen,
would doing that still work if I just left
the Python-Python sitting there?)
Python-Python installed a couple of symlinks into /usr/local/bin and then put
/usr/local/bin in the front of your $PATH environment variable so it gets picked
up first on the terminal. For my Python-Python 2.5, I have

python
python2.5
pythonw
pythonw2.5
python-config
python2.5-config

Remove these and then when you type "python" at the terminal, you will get
/usr/bin/python, which points to the Apple-Python.
OR: Is there something special I could do with
CoreGraphics scripts and/or how I invoke them
so that _they_ would use the Apple-Python while
other scripts would just use the Python-Python
as now?
For scripts executed from the terminal, you could start them with a hash-bang line:

#!/usr/bin/python

Use "chmod u+x" on the script, and then you can execute it like any other
program from the terminal.
(A last choice would be a setup where most
scripts work as they do now and I somehow
make CoreGraphics work from Terminal but
not in Finder...)

(Hmm, a wild guess: something somewhere is an
alias to Python-Python, and I just change that
(how?) to an alias to Apple-Python?)

I'm a little nervous about making any changes,
because something very mysterious happened when
I was setting things up the way they are now -
since I have no idea what I did to make things
work the way they are now I don't know that
I could set things up the way they are now
again if I had to. If you can explain the
following that will be something (I've been
meaning to ask about the following sometime,
hasn't been important til now when I'm
contemplating changing the setup):

The history: I get a Mac. I discover that it
includes Python, can't figure out how to
execute .py files in Finder. I hear about
MacPython.

I install the "small" MacPython download, that's
supposed to just add ApplicationLauncher(?),
Idle, etc on top of the existing Python.
Works fine _except_ that when I double-click
a .py file in Finder it executes with the
cwd equal to the root directory instead of
the directory where the script is located.

Trying to fix that I install the "full"
MacPython, including Python itself. Doesn't
help, scripts still execute in "/".

A few weeks later I decide to try to fix that.
The plan is to edit whatever.py, the script
that's supposed to run before everything else
allowing customizations (always takes me a
while to find the magic name in the docs -
probably you know the name I mean). The plan
is to extract the directory I want from
sys.argv and then chdir.

And here's the mysterious part: The day I
plan on modifying whatever.py I find the
problem has fixed itself! When I double-
click .py files they execute in the right
directory.

If you asked what I was smoking I wouldn't blame
you. (Not that I really think that's it, but
the _only_ thing I can imagine I did that
could have led to the change was that perhaps
I hadn't yet tried out Idle when things were
executing in the wrong directory, and somehow
the first time I ran Idle it fixed something
for me.)

Anyway. You have any idea what was going on there,
and/or any idea about what to do to fix that
problem if it appears again?
I'm afraid I don't. I don't use ApplicationLauncher.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Nov 2 '07 #5

P: n/a
On Fri, 02 Nov 2007 14:09:25 -0500, Robert Kern
<ro*********@gmail.comwrote:
>David C. Ullrich wrote:
>[...]

So CoreGraphics is a builtin in Apple-Python,
explaining why I didn't find the relevant
CoreGraphics.py anywhere on the hard drive, eh?

Okay, which version of OS X do you have? In 10.3 and 10.4 it used to be here:
/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/plat-mac/CoreGraphics.py
I'm using 10.4. Yesterday I checked that there is indeed a

/System/Library/Frameworks/Python.framework

but I didn't look any deeper because I already "knew"
that CoreGraphics.py wasn't there. Come to think of
it, it could well be that it's there and I didn't find
it because Spotlight didn't want to show me things
inside /System? I'll check. (I'll check "soon" -
again, I'm at home and the Mac's at the office.)
>I notice that in 10.5, it no longer exists, though.
>[How do I get to the Python I want?]

Python-Python installed a couple of symlinks into /usr/local/bin and then put
/usr/local/bin in the front of your $PATH environment variable so it gets picked
up first on the terminal. For my Python-Python 2.5, I have

python
python2.5
pythonw
pythonw2.5
python-config
python2.5-config

Remove these and then when you type "python" at the terminal, you will get
/usr/bin/python, which points to the Apple-Python.
Thanks. Right this second I have no idea _where_ I should
go to "remove" that, but I can probably find out. It's
in .bashrc or somewhere, I bet, No, don't tell me...
>
>OR: Is there something special I could do with
CoreGraphics scripts and/or how I invoke them
so that _they_ would use the Apple-Python while
other scripts would just use the Python-Python
as now?

For scripts executed from the terminal, you could start them with a hash-bang line:

#!/usr/bin/python

Use "chmod u+x" on the script, and then you can execute it like any other
program from the terminal.
Curiously, I did make one of the sample scripts executable and it
still didn't work. I _think_ the hash-bang at the start was
usr/bin/python (I did verify yesterday that /usr/bin/python is
pointing to ApplePython.) Again, don't tell me...
>[ApplicationLauncher mystery]

I'm afraid I don't. I don't use ApplicationLauncher.
Thanks anyway - this has all been very informatiive.

************************

David C. Ullrich
Nov 3 '07 #6

P: n/a
On Fri, 2 Nov 2007 13:14:16 +0100, Tommy Nordgren
<to************@comhem.sewrote:
>
On 2 nov 2007, at 02.10, David C. Ullrich wrote:
>[Why doesn't CoreGraphics work?]
--
http://mail.python.org/mailman/listinfo/python-list
There are Python wrappers for the Cocoa API. These can be used with
binaries from Python.org.
The name of the module is PyObjC, and can be downloaded from
sourceforge.
Ah. I'd read about this in the context of using Python in Xcode
applications, which seems like something I definitely want to
learn how to do, but it was scheduled for "later"...

PyObjC will also allow a person to access Cocoa things from
an ordinary Python script? If so, keen.

Thanks.
>There are binary builds for Python up to version 2.4.1.
For Python 2.5.1, it is necessary to build it yourself, which I've
found out requires modifying setup.py.
(The problem is that setup.py tries to use two modules that's not
installed by default on Tiger)
------
What is a woman that you forsake her, and the hearth fire and the
home acre,
to go with the old grey Widow Maker. --Kipling, harp song of the
Dane women
Tommy Nordgren
to************@comhem.se


************************

David C. Ullrich
Nov 3 '07 #7

P: n/a
David C. Ullrich wrote:
On Fri, 02 Nov 2007 14:09:25 -0500, Robert Kern
<ro*********@gmail.comwrote:
>David C. Ullrich wrote:
>>[...]

So CoreGraphics is a builtin in Apple-Python,
explaining why I didn't find the relevant
CoreGraphics.py anywhere on the hard drive, eh?
Okay, which version of OS X do you have? In 10.3 and 10.4 it used to be here:
/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/plat-mac/CoreGraphics.py

I'm using 10.4. Yesterday I checked that there is indeed a

/System/Library/Frameworks/Python.framework

but I didn't look any deeper because I already "knew"
that CoreGraphics.py wasn't there. Come to think of
it, it could well be that it's there and I didn't find
it because Spotlight didn't want to show me things
inside /System? I'll check. (I'll check "soon" -
again, I'm at home and the Mac's at the office.)
Possibly. The "locate" command on the terminal is usually more reliable.
>I notice that in 10.5, it no longer exists, though.
>>[How do I get to the Python I want?]
Python-Python installed a couple of symlinks into /usr/local/bin and then put
/usr/local/bin in the front of your $PATH environment variable so it gets picked
up first on the terminal. For my Python-Python 2.5, I have

python
python2.5
pythonw
pythonw2.5
python-config
python2.5-config

Remove these and then when you type "python" at the terminal, you will get
/usr/bin/python, which points to the Apple-Python.

Thanks. Right this second I have no idea _where_ I should
go to "remove" that, but I can probably find out. It's
in .bashrc or somewhere, I bet, No, don't tell me...
$ rm /usr/local/bin/python /usr/local/bin/python2.5 ...

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Nov 3 '07 #8

P: n/a
On Fri, 02 Nov 2007 14:09:25 -0500, Robert Kern
<ro*********@gmail.comwrote:
>David C. Ullrich wrote:
>[???]

Okay, which version of OS X do you have? In 10.3 and 10.4 it used to be here:
/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/plat-mac/CoreGraphics.py

I notice that in 10.5, it no longer exists, though.
Um, surely that doesn't mean that there's no CoreGraphics
available in 10.5?
>>[...]

For scripts executed from the terminal, you could start them with a hash-bang line:

#!/usr/bin/python

Use "chmod u+x" on the script, and then you can execute it like any other
program from the terminal.
Sure enough: As I suspected yesterday morning, the reason that didn't
work the first time I tried it is that I'd already "corrected" the
import statement to Carbon.CoreGraphics. When I put it back the way
it's supposed to be everything's great.

Such a happy camper - I now have a pdf consisting of a blank white
page with one red circle in the middle! Something I've always
wanted - thanks.

************************

David C. Ullrich
Nov 4 '07 #9

P: n/a
David C. Ullrich wrote:
On Fri, 02 Nov 2007 14:09:25 -0500, Robert Kern
<ro*********@gmail.comwrote:
>David C. Ullrich wrote:
>>[???]
Okay, which version of OS X do you have? In 10.3 and 10.4 it used to be here:
/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/plat-mac/CoreGraphics.py

I notice that in 10.5, it no longer exists, though.

Um, surely that doesn't mean that there's no CoreGraphics
available in 10.5?
The CoreGraphics C library still exists, of course. The Python module they
provided does not.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Nov 4 '07 #10

P: n/a
On Sun, 04 Nov 2007 15:56:21 -0600, Robert Kern
<ro*********@gmail.comwrote:
>David C. Ullrich wrote:
>On Fri, 02 Nov 2007 14:09:25 -0500, Robert Kern
<ro*********@gmail.comwrote:
>>David C. Ullrich wrote:
[???]
Okay, which version of OS X do you have? In 10.3 and 10.4 it used to be here:
/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/plat-mac/CoreGraphics.py

I notice that in 10.5, it no longer exists, though.

Um, surely that doesn't mean that there's no CoreGraphics
available in 10.5?

The CoreGraphics C library still exists, of course. The Python module they
provided does not.
Right - I was referring to the Python module.

Why would they do such a thing? (Never mind, I don't
imagine you know.)

Hmm, I saw somewhere the other day that PyObjC comes with 10.5.
Maybe that means there's no longer any point to a separate
CoreGraphics.py?
************************

David C. Ullrich
Nov 5 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.