Python framework builds, pipenv, pyenv and virtualenv…

Ryan Adams
2 min readJun 30, 2021

On a Mac, there are two different types of Python; a regular build and a framework build. If you need to do anything involving a GUI (say perhaps Matplotlib, or wxPython) then you might find you need to use a framework build (see here for an old piece of relevant documentation).

No-one in their right mind uses the system provided version of Python on a Mac so you probably have a regular build installed. You’ll need a different build to get the framework version.

Now, if you want to run different versions of python you will probably be using something like pyenv. Pyenv allows you to have multiple independent python versions on your system without breaking things.

To install a framework build with pyenv you’ll run something like

$ env PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install 3.9.6

It’s the — enable-framework piece that’s significant (see https://github.com/pyenv/pyenv/issues/99)

You’ll just need to somehow remember which Python is a framework build and which isn’t…

Great. Now your wxPython GUI application will work.

But our problems don’t stop there.

Pipenv is the currently recommended tool for managing dependencies. It uses a pipfile to let you manage dependencies usefully. It also integrates with virtualenv to let you isolate your environment. All good.

However, when you need a framework build of Python that you’ve installed using pyenv you’ll sometimes see an error like this:

➜ space-invitations git:(main) ✗ pipenv installCreating a virtualenv for this project…Pipfile: /Users/ryan/Projects/space-invitations/PipfileUsing /Users/ryan/.pyenv/versions/3.9.1/bin/python3.9 (3.9.1) to create virtualenv…⠹ Creating virtual environment…Already using interpreter /Users/ryan/.pyenv/versions/3.9.1/Python.framework/Versions/3.9/bin/python3.9
Using base prefix ‘/Users/ryan/.pyenv/versions/3.9.1/Python.framework/Versions/3.9’
New python executable in /Users/ryan/.local/share/virtualenvs/space-invitations-XHThpeOX/bin/python3.9
Not overwriting existing python script /Users/ryan/.local/share/virtualenvs/space-invitations-XHThpeOX/bin/python (you must use /Users/ryan/.local/share/virtualenvs/space-invitations-XHThpeOX/bin/python3.9)
ERROR: The executable /Users/ryan/.local/share/virtualenvs/space-invitations-XHThpeOX/bin/python3.9 is not functioningERROR: It thinks sys.prefix is ‘/Users/ryan/.pyenv/versions/3.9.1/Python.framework/Versions/3.9’ (should be ‘/Users/ryan/.local/share/virtualenvs/space-invitations-XHThpeOX’)ERROR: virtualenv is not compatible with this system or executableRunning virtualenv with interpreter /Users/ryan/.pyenv/versions/3.9.1/bin/python3.9✘ Failed creating virtual environment[pipenv.exceptions.VirtualenvCreationException]:Failed to create virtual environment.

I’ve forgotten how to fix this twice now, so I’m writing this in order to ensure I find it the next time.

The problem here is that Pipenv creates a virtualenv but the virtualenv doesn’t support using the framework build of Python.

The solution seems to be that Pipenv needs to use venv instead of virtualenv as discussed here but that seems unlikely to happen.

Anyway, the workaround is provided in this comment — simply create the virtual environment using venv first, then run pipenv install and it will work as you require.

python3 -m venv .venv
pipenv install foo # all good.

--

--

Ryan Adams

Part time man-in-shed. Cul-de-sac dweller. Perpetual newbie. Wannabe Woodsman. Flâneur. Software trainer