Fixing the Leopard install of Python, and virtualenv for the win.

December 16th, 2007 § 9 comments

So, as a fol­lowup to my pre­vi­ous post — PyObjc 2: Leop­ard, Python 2.5.1 and You. A com­menter pointed out that the python.org instal­la­tion of python only adds a new ver­sion of Python to the /Library/Frameworks/ direc­tory — it does not replace the default one in /System/Library/Frameworks/.

So, in order to “return” to my “pris­tine” leop­ard state, all I actu­ally had to do was remove this line in my .bash_profile (which I hadn’t noticed — my .bash_profile is about 250 lines long):

PATH="/Library/Frameworks/Python.framework/Versions/Current/bin:${PATH}"

Then I sim­ply moved the /Library/Frameworks/Python.framework direc­tory to some other name (just to be anal). The only side-effect being that I “lost” all of my installed python mod­ules in the python.org install’s site-packages directory.

The lat­ter is fine, because I’ve been mean­ing to ‘clean house’ with the cruft I’ve built up com­pul­sively down­load­ing stuff from pypi and doing other work. Since I’m now deal­ing with a ‘cleaned out’ ver­sion, I took the chance to clean-install vir­tualenv and setup a nice /jesse/python_vms direc­tory. I added a ‘default’ pro­file that gets auto-loaded on login, and added a few func­tions to let me switch in-between them. Exam­ple:

# Python VMs override(s)
VMDIR="/Users/jesse/python_vms"
DEFAULT_PY_ENV="$VMDIR/default"
SANDBOX_PY_ENV="$VMDIR/sandbox"
# Load the default python env by, uh, default!
. $DEFAULT_PY_ENV/bin/activate
pydefault () { . $DEFAULT_PY_ENV/bin/activate; }
pysandbox () { . $SANDBOX_PY_ENV/bin/activate; }

In my case, the default pro­file will only load my work-profile, while my sand­box is going to be where all the pypi mod­ules I tin­ker with go. The nice thing of course, is that the acti­vate script included with the vir­tualenv env over­rides my default $PATH to make sure the local bin direc­tory is used prior to the main $PATH, which means I don’t need to worry about con­flicts with the default easy_install that comes with Leopard.

Now I’ve got the best of all worlds, the default install that has all the leop­ard pyobjc good­ies, and a nice clean sandbox.

Of course, this doesn’t even cover the hor­ror which is my /subversion direc­tory — which at last look, I had to write a shell-script wrap­per to auto-update the lar­gish num­ber of svn projects I watch/sync.

  • http://www.doughellmann.com/ Doug Hell­mann

    I’ve been lov­ing vir­tualenv, and your sand­box switch­ing scripts sound like an improve­ment over typ­ing the long paths out by hand each time I want to change my active environment.

  • jnoller

    Using bash func­tions in your pro­file allows for tab com­ple­tion on the com­mand too. I use a lot of one lin­ers to call into python code snip­pets I use a lot, ala:

    magic () { python –c “import foo; foo”; }

  • Ed

    Hi, I don’t want to be another one of those peo­ple who post help requests in blog com­ments but I was won­der­ing how you got vir­tualenv work­ing on Leop­ard?
    Did it “Just Work”?

    I’ve tried cre­at­ing envi­ron­ments with both ver­sion 0.9.2 and from SVN with the clean python install from Leop­ard and none of my envi­ron­ments are ref­er­ences the loca­tion of the glob­ally installed pack­ages. I just get absolutely clean envi­ron­ments every time with­out the PyObjC stuff!

    I’m quite new at this ;) do you have any ideas?
    thx

  • jnoller

    I think I see what you mean:

    (default)woot:python_vms jesse$ python
    impor­Python 2.5.1 (r251:54863, Oct 5 2007, 21:08:09)
    [GCC 4.0.1 (Apple Inc. build 5465)] on dar­win
    Type “help”, “copy­right”, “cred­its” or “license” for more infor­ma­tion.
    »> import WebKit
    Trace­back (most recent call last):
    File “<stdin>”, line 1, in <mod­ule>
    ImportEr­ror: No mod­ule named WebKit
    »> ^D
    (default)woot:python_vms jesse$ de
    deac­ti­vate declare defaults desdp dev_mkdb
    (default)woot:python_vms jesse$ deac­ti­vate
    woot:python_vms jesse$ python
    Python 2.5.1 (r251:54863, Oct 5 2007, 21:08:09)
    [GCC 4.0.1 (Apple Inc. build 5465)] on dar­win
    Type “help”, “copy­right”, “cred­its” or “license” for more infor­ma­tion.
    »> import WebKit
    »> ^D
    woot:python_vms jesse$

    I am going to dig into this more

  • jnoller

    I see the prob­lem. It’s an easy fix, virtualenv.py is not pulling the pyObjc stuff into the sys.path, which I did not notice.

    »> sys.path.append(‘/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/PyObjC’)
    »> import WebKit
    »>

    Fixes it, I am going to poke at the vir­tualenv source to see if I can fix it.

  • Ed

    i may be mis­taken but doesn’t the sys.path.append method make the sys­tem look in the sys­tem pack­ages folder first rather than the envi­ron­ments pack­ages first? (ie lat­est men­tioned path first)
    there­fore if you add that path it’ll use any sys­tem installed ver­sion before it uses your versions?

  • Ed

    not sure that last com­ment made sense, but i’m sure you know what i mean :)

  • Ed

    i may be mis­taken but doesn’t the sys.path.append method make the sys­tem look in the sys­tem pack­ages folder first rather than the envi­ron­ments pack­ages first? (ie lat­est men­tioned path first)
    there­fore if you add that path it’ll use any sys­tem installed ver­sion before it uses your versions?

  • Ed

    not sure that last com­ment made sense, but i’m sure you know what i mean :)

What's this?

You are currently reading Fixing the Leopard install of Python, and virtualenv for the win. at jessenoller.com.

meta