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

So, as a followup to my previous post – PyObjc 2: Leopard, Python 2.5.1 and You. A commenter pointed out that the python.org installation of python only adds a new version of Python to the /Library/Frameworks/ directory – it does not replace the default one in /System/Library/Frameworks/.

So, in order to “return” to my “pristine” leopard state, all I actually 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 simply moved the /Library/Frameworks/Python.framework directory to some other name (just to be anal). The only side-effect being that I “lost” all of my installed python modules in the python.org install’s site-packages directory.

The latter is fine, because I’ve been meaning to ‘clean house’ with the cruft I’ve built up compulsively downloading stuff from pypi and doing other work. Since I’m now dealing with a ‘cleaned out’ version, I took the chance to clean-install virtualenv and setup a nice /jesse/python_vms directory. I added a ‘default’ profile that gets auto-loaded on login, and added a few functions to let me switch in-between them. Example:

# 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 profile will only load my work-profile, while my sandbox is going to be where all the pypi modules I tinker with go. The nice thing of course, is that the activate script included with the virtualenv env overrides my default $PATH to make sure the local bin directory is used prior to the main $PATH, which means I don’t need to worry about conflicts 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 leopard pyobjc goodies, and a nice clean sandbox.

Of course, this doesn’t even cover the horror which is my /subversion directory – which at last look, I had to write a shell-script wrapper to auto-update the largish number of svn projects I watch/sync.

  • Ed
    Hi, I don't want to be another one of those people who post help requests in blog comments but I was wondering how you got virtualenv working on Leopard?
    Did it "Just Work"?

    I've tried creating environments with both version 0.9.2 and from SVN with the clean python install from Leopard and none of my environments are references the location of the globally installed packages. I just get absolutely clean environments every time without the PyObjC stuff!

    I'm quite new at this ;) do you have any ideas?
    thx
  • I see the problem. 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 virtualenv source to see if I can fix it.
  • Ed
    i may be mistaken but doesn't the sys.path.append method make the system look in the system packages folder first rather than the environments packages first? (ie latest mentioned path first)
    therefore if you add that path it'll use any system installed version before it uses your versions?
  • Ed
    not sure that last comment made sense, but i'm sure you know what i mean :)
  • I think I see what you mean:

    (default)woot:python_vms jesse$ python
    imporPython 2.5.1 (r251:54863, Oct 5 2007, 21:08:09)
    [GCC 4.0.1 (Apple Inc. build 5465)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import WebKit
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ImportError: No module named WebKit
    >>> ^D
    (default)woot:python_vms jesse$ de
    deactivate declare defaults desdp dev_mkdb
    (default)woot:python_vms jesse$ deactivate
    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 darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import WebKit
    >>> ^D
    woot:python_vms jesse$

    I am going to dig into this more</module></stdin>
  • I've been loving virtualenv, and your sandbox switching scripts sound like an improvement over typing the long paths out by hand each time I want to change my active environment.
  • Using bash functions in your profile allows for tab completion on the command too. I use a lot of one liners to call into python code snippets I use a lot, ala:

    magic () { python -c "import foo; foo"; }
blog comments powered by Disqus