Friday, September 25, 2009

wxPython in a virtualenv

virtualenv is an absolutely wonderful tool. I use it every day in so many ways, and enjoy the freedom it gives to experiment with anything. Found a new tool, and want to try it out, but don't want to screw up your system's Python libraries? Try it out in a virtualenv!

Unfortunately, getting wxPython in a virtualenv is a painful process, and definitely takes some work. Here's the steps to do so:

  1. Download this patch, and save it as $HOME/wxpatch.txt.Without this patch, wxPython2.8.10.1 will fail to compile properly. Hopefully, this will be fixed in the next release.
  2. Make your virtualenv. If you're like me, you're going to be using virtualenvwrapper, so you'll run this command:

    mkvirtualenv --no-site-packages wxpython

  3. Activate your virtualenv. If you are using the virtualenv wrapper I linked above, you would do "workon wxpython". Otherwise, it will be "source $PATH_TO_VENV/bin/activate".
  4. Download the wxPython source and save it as $HOME/wxPython-src-
  5. Now comes the messy command. Yes, all of these are genuinely necessary in order to make this work properly. Make sure to substitue "$PATH_TO_VENV" with the appropriate real path to the root of the virtual environment you made in step 2. Warning: The next steps will take a long time.

    cd $HOME
    tar -xjf wxPython-src-
    cd wxPython-src-
    patch -p0 < $HOME/wxpatch.txt
    ./configure --prefix=$PATH_TO_VENV --with-gtk=2 --enable-unicode --with-opengl
    make install
    cd contrib/src/stc
    make install
    cd ../gizmos
    make install
    cd ../../../wxPython
    python install 

  6. Finally, you will need to ensure that your LD_LIBRARY_PATH variable is configured. If you are using the virtualenv wrappers mentioned above, create a script named $PATH_TO_VENV/bin/postactivate that has the following contents (no substitutions of any sort here at all!):


    Now, set the script to be executable.

  7. If you're not using the virtualenv wrappers mentioned above, edit $PATH_TO_VENV/bin/activate and add that same line at the end of the file.
With all of the above done, you only have to activate the virtualenv, and you can then import and use wx inside of it without problem.


thouis said...

Under OS X, I usually just install from the dmg and then copy the wxredirect.pth into the virtualenv's site-packages. Is that a bad idea?

stuaxo said...

The wxredirect.pth method should be fine.

Note - if not on OSX then you will have to modify the path in it.

If actually installing using the method listed above using $VIRTUAL_ENV to get the root of the site packages should work.