I’m doing some Python profiling
and I wanted to use the Run­SnakeRun utility to view the profile data.
Un­for­tu­nate­ly, that’s not straight­for­ward on Mac OS X if you use a virtualenv,
and it’s even less easy if you’re using the Python
installed by the Homebrew (brew) package manager.

There are several problems:

Installing wxPython on OS X 10.10.
This is the cross-platform GUIAPI toolkit used by Run­SnakeRun.

Getting wxPython installed in a virtualenv

Running wxPython apps in a virtualenv on the Mac.

Installing wxPython

I downloaded wxPython3.0-osx-3.0.2.0-cocoa-py2.7.dmg,
released in November 2014.

If you open the DMG and attempt to run the PKG,
you will likely get a misleading error message from OS X:

“wxPython3.0-osx-cocoa-py2.7.pkg” is damaged and can’t be opened.
You should eject the disk image.

Open the System Pref­er­ences app, click Security & Privacy, then General.
Click the Lock icon to make changes,
then change Allow apps downloaded from to Anywhere.
Now try running the wxPython PKG again to install wxPython.
Afterwards, you should reset Allow apps downloaded from to its previous value.

Installing and running wxPython in a virtualenv

As I learned from earlier work by Mike Soulier and Robin Dunn,
there are two issues in running wxPython apps in a virtual en­vi­ron­ment.

First, you need the wx.pthpath con­fig­u­ra­tion file to be in your
virtualenv’s site-packages directory.
The solution is to copy the installed wxredirect.pth.

Second, the Python binary installed in the virtualenv is not a framework binary,
and so wxPython cannot access the display or create GUI objects.
The solution is to set PYTHONHOME to the virtualenv,
then use the framework binary to run the wxPython app.

Previous Work

The following is an attempt from last year at setting up wxPython in a virtualenv,
which no longer works.
It sets up a script called fwpy in the virtualenv,
which allows you to run a wxPython app with the framework Python.

#!/usr/bin/env python# Adapted from http://wiki.wxpython.org/wxPythonVirtualenvOnMacimportos,sysimportoptparsefromdistutils.sysconfigimportget_python_libsite_packages=get_python_lib()PYTHON_VERSION=sys.version[:3]# e.g., "2.7"ifos.getenv('VIRTUAL_ENV'):print>>sys.stderr,"ERROR: You must *not* run this inside of a virtualenv. Run 'deactivate' first."sys.exit(1)parser=optparse.OptionParser(description="virtualenv-aware wrapper for wxPython.",usage="usage: %prog virtualenv-name-or-path")(options,args)=parser.parse_args()venv=argsandargs[0]defvalid_virtualenv_dir(venv):returnvenvandos.path.exists(os.path.join(venv,"bin","activate"))ifnotvalid_virtualenv_dir(venv):# Using virtualenvwrapper?ifos.getenv("WORKON_HOME"):venv=os.path.join(os.getenv("WORKON_HOME"),venv)ifnotvalid_virtualenv_dir(venv):print"Can't find virtualenv",venvsys.exit(1)framework_python="""\#!/bin/bash# Invoke Framework Python inside of a virtualenv; e.g., for wxPython[ -z "$VIRTUAL_ENV" ] && echo "Not running inside of a virtualenv" && exit 1PYTHONHOME="$VIRTUAL_ENV" exec "%s" "$@""""%sys.executablewx_pth=Noneforwx_pth_filein[os.path.join(site_packages,'wx.pth'),'/Library/Python/2.7/site-packages/wxredirect.pth']:ifos.path.exists(wx_pth_file):withopen(wx_pth_file)asf:wx_pth=f.read().splitlines()[0]break# This is the value of wx.pth for the system pythonabs_wx_pth=os.path.join(site_packages,wx_pth)fwpy=os.path.join(venv,"bin","fwpy")withopen(fwpy,"w")asf:f.write(framework_python)os.chmod(fwpy,0755)print"Wrote",fwpy# TODO: copy wx.pthtarget_wx_pth=os.path.join(venv,"lib","python"+PYTHON_VERSION,"site-packages","wx.pth")withopen(target_wx_pth,"w")asf:f.write(abs_wx_pth)print"Wrote",target_wx_pth