How to install wxPython in a Python virtual environment on Debian Buster

wxPython logoLately, I’ve been brushing up on my Python skills, and decided to use wxPython for some GUI programming.  But, I had some trouble installing wxPython in a Python virtual environment on Debian, so once I figured it out, I’ve recorded what I had to do in case anyone else needs to do the same thing.

Note that if you’re NOT using a virtual environment, it’s pretty easy: just install the “python3-wxgtk4.0” package.  Of course, if you’re using Python 2, you’ll need something else, but because Python 2 has reached end-of-life, I don’t know why you’d want to use that!  Anyways, just as a refresher, to install a Debian package, you’ll need to run the following from the command line as root:

apt-get install python3-wxgtk4.0

You may, of course, need to run it with sudo to run as root:

sudo apt-get install python3-wxgtk4.0

In this post, rather than saying whether to run something as root or as a regular user, I’m just going to prepend the root commands with “sudo”; if you get root access another way, do that instead.

OK, but as I said, I want to install wxPython in a Python virtual environment.  The problem comes because Python needs to compile wxPython, so you need a number of development packages installed.  You actually need hundreds of packages (!), but I’ve narrowed it down to a short list, and the other packages will be installed as dependencies of these ones.

Note that I am currently running Debian Buster.  I’ve tried this out with a basic non-GUI install of Buster, as well as with the XFCE desktop installed (which I set up via “sudo tasksel install xfce-desktop”).

First thing you need to do if you’re going to use Python virtual environments on Debian is to install the Python virtual environment package:

sudo apt-get install python3-venv

Now, you can create your Python virtual environment:

python3 -m venv example-env

This will create your Python virtual environment in a directory called example-env.

Now, install all of required Debian development packages:

sudo apt-get install build-essential python3-dev \
  libwebkit2gtk-4.0-dev libtiff-dev libnotify-dev \
  freeglut3-dev libsdl1.2-dev libgstreamer-plugins-base1.0-dev

This will cause hundreds of packages to be installed, so don’t be surprised when that happens!

Once you’ve got those installed, you’re almost all set.  Activate your Python virtual environment:

source example-env/bin/activate

Using Python’s “pip” program, install the “wheel” Python package into the virtual environment:

pip install wheel

And now, finally, you can install wxPython:

pip install wxPython

Be aware that this is going to result in the wxPython package being compiled, which can take a LONG time.  Don’t be surprised if it takes an hour or more.  But, once it’s successfully installed, you can use wxPython in your virtual environment!  I tested it out by running the helloworld2.py program found on the wxPython Overview page.

Have fun!