Accessing Keychain from PyObjC
Sun Sep 20, 2009 · 206 words

10.5 came bundled with PyObjC, but in 10.6 it's actually usable since we now have Python 2.6.1 (with “batteries included”). This makes Python more and more the language of choice for your everyday Cocoa hacking needs.

One of the problems you'll run into quite early on is accessing the Keychain which is still firmly in Carbon aka procedural C land. Maybe the PyObjC bridge also allows calling C stuff, I don't know, but lucky for us, we don't have to. There's been posts about this elsewhere but I think I've found a more simple solution. One that doesn't involve ancient frameworks or changes to your Python imports.

So, to access the Keychain from your PyObjC app:

which should result in this in Keychain Access:

And reading a password is just as simple:

ki = kp.genericKeychainItemForService_withUsername_("someservice", "filipp")
pw = ki.password()

And that's it! All kudos and many thanks to Brian Amerige of Extendmac for sharing his Keychain code!

back · essays · credits ·