[CentralOH] Virtualenv and Idle on the Mac
Mark Erbaugh
mark at microenh.com
Wed Dec 14 21:51:29 CET 2011
I use virtualenv for most of my Python development. I also like the Idle Python IDE. Unfortunately, on Mac OSX 10.6, IDLE is seriously broken. See http://www.python.org/download/mac/tcltk/. Even in a virtualenv, the default idle is /usr/bin/idle which runs doesn't respect the virtualenv and has a bunch of other problems, so I copy my idle script to the bin directory of the virtualenv. Here's the idle script I came up with:
> #! /usr/bin/env python
>
> import os
>
> def fork_idle():
> pid = os.fork()
> if pid == 0:
> from idlelib import macosxSupport
> macosxSupport._appbundle = True
> from idlelib.PyShell import main
> main()
>
> if __name__ == '__main__':
> fork_idle()
This forks a process so that once I launch idle in a terminal window, I can continue to use the terminal window and so that idle will continue to run if I close the terminal window. It also causes idle to run as an OSX app which slightly changes the GUI appearance to comply with Mac OSX guidelines.
The one thing that is still broken with this script is the Python Docs option under the Help menu. This loads the documentation from the python.org website rather than the local copy into the web browser. This is setup in idlelib.EditorWindow and I determined that if I set up a symlink to /Library/Frameworks/Python.framework/Versions/2.7/Resources named Resources in the virtualenv home folder the Python Docs option would load the local copy.
So my process was create a virtualenv, copy my idle script to the bin folder and create the symlink in the home folder. I poked around in virtualenv.py and found that if present, it will call an after_install function. Here's what I came up with that automatically sets up idle in the new virtualenv:
> IDLE = """#! /usr/bin/env python
>
> import os
>
> def fork_idle():
> pid = os.fork()
> if pid == 0:
> from idlelib import macosxSupport
> macosxSupport._appbundle = True
> from idlelib.PyShell import main
> main()
>
> if __name__ == '__main__':
> fork_idle()"""
>
>
> def after_install(options, home_dir):
> idle_name = os.path.join(home_dir, 'bin', 'idle')
> with open(idle_name, 'wt') as f:
> f.write(IDLE)
> os.chmod(idle_name, 0755)
> os.symlink('/Library/Frameworks/Python.framework/Versions/2.7/Resources',
> os.path.join(home_dir, 'Resources'))
I hope this helps someone.
Mark
BTW, I normally use vi to edit Python files, but for poking around and trying things out idle is quite useful. When I edit virtualenv.py in vi, I discovered an anomoly. I have syntax coloring enabled and at the top of the file it works fine. If I jump to the bottom, the syntax coloring is messed up. Above the if __name__ == '__main__' idiom at the bottom of the file there are several base64 encoded binary strings. It seems like one (or more) of these is messing up the syntax coloring. Hitting ctrl-L from the bottom of the file doesn't fix the syntax coloring, but if I page up (ctrl-B) a page and hit ctrl-L it does fix syntax coloring.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/mailman/private/centraloh/attachments/20111214/02a062d5/attachment.html>
More information about the CentralOH
mailing list