Python – Wx can’t run on mac

anacondamacospythonwxpython

I installed anaconda instead of system's python on mac,but when I type

import wx
app = wx.App()

I got this:

This program needs access to the screen. Please run with a Framework
build of python, and only when you are logged in on the main display
of your Mac.

I use the script:

#!/bin/bash

# what real Python executable to use
PYVER=2.7
PYTHON=/Library/Frameworks/Python.framework/Versions/$PYVER/bin/python$PYVER

# find the root of the virtualenv, it should be the parent of the dir this script is in
ENV=`$PYTHON -c "import os; print os.path.abspath(os.path.join(os.path.dirname(\"$0\"), '..'))"`

# now run Python with the virtualenv set as Python's HOME
export PYTHONHOME=$ENV 
exec $PYTHON "$@"

but it just used the system python.Can't use the lib in anaconda.
I want to use the wx GUI in anaconda,how to solve the problem?

Best Answer

I fixed the issue for my Python 2 and 3 virtual envs by reading this post (see my tip in the followed):

My environment is:

  • macOS 10.12.5
  • Python 2 installed by Homebrew
    • wxPython installed through: brew insstall wxpython
  • Python 3 installed by Homebrew
    • wxPython installed through: gpip3 install wxpython (the global PIP for Python 3)

Tips:

  • You could modify the script in that post to work for Python 3.
  • You should set "PYTHONHOME" in the "activate" file instead of "postactivate" if you DO NOT use virtualenvwrapper.