wxPython in virtualenvs on Catalina

Hello,

I am trying to get wxPython to work on Catalina using the same setup that I had on older Mac versions, however I am running into some issues. I’ve done a bunch of searching online, but I couldn’t find anything useful or recent about this.

My setup is as follows:

I’ve installed the framework version of Python 3.7 and the xcode dev tools.
I’ve installed virtualenv as pip3 install --user virtualenv.

I create a virtual environment as follows:

> cd ~/Desktop/test
> virtualenv env
> source env/bin/activate

This runs python 3.7, but not the framework version:

> python --version
Python 3.7.3

However, wxpython needs the framework version of python to work.

On older Mac versions I could get around this by following this guide: https://wiki.wxpython.org/wxPythonVirtualenvOnMac.

That guide recommends setting PYTHONHOME to the virtualenv and manually running the framework exectuable. That is, I can do this:

> export PYTHON = /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7
> export PYTHONHOME = ~/Desktop/test/env/
> $PYTHON my_wxpython_script.py

And this runs fine on older Mac versions.

However, on Catalina I now get the following error:

Fatal Python error: initfsencoding: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'

Current thread 0x0000000115ce9dc0 (most recent call first):

Any ideas how I can fix the error above, or, alternatively, what is the recommended way of running the framework Python in a virtualenv on Catalina?

Don’t use virtualenv. Instead use the venv package included with Python3. In practice, it is very similar to virtualenv, although there are some differences under the hood.

python3.7 -m venv env
source env/bin/activate

Ok, thanks, I’ll try that. Does the rest of the setup (using PYTHONHOME) make sense?

It shouldn’t be necessary, venv takes care of everything.

1 Like

Indeed it does. Thanks for your help!