wxPython: This program needs access to the screen

This resolves the problem, but it can't be the prettiest solution:

# install anaconda
install anaconda

# uninstall all versions of pythonWx
pip uninstall pythonWx -y
conda remove pythonwx

# install the python.app binary through conda
conda install python.app

# determine where the conda binary lives
which conda

# that previous command returns something like: 
# /Users/yaledhlab/anaconda3/bin/conda
# replace the /conda with /python.app
# and run the result in a terminal
/Users/yaledhlab/anaconda3/bin/python.app

# that should open a Python terminal (you know you're in the Python
# terminal if you see >>> as a prefix for your shell)
# import the python package manager and install wxPython to
# your python.app version of Python
import pip
pip.main(['install', 'wxPython'])

# exit the python interpreter
exit()

# run the program
/Users/yaledhlab/anaconda3/bin/python.app main.py

wxPython on Mac within a virtual environment throws this error, as explained by wxPython website here: https://wiki.wxpython.org/wxPythonVirtualenvOnMac

If you are not running it in a virtual environment and still receive this error, try running your script that uses wxpython with "pythonw" instead of "python". Ex:

pythonw hello.py

^See section "4.1.2 Running Scripts with a GUI" (on MacOS) from the following page in the Python Docs to see this python quirk explained: https://docs.python.org/3/using/mac.html


I always had it working, including after update to Catalina and I never needed to run some silly pythonw instead of python and had all my wx-enabled scripts running correctly.

At some time something broke in my environment, I think it was the pip-tools that without any warning started to remove my packages. Not sure what happened but damage has been done and suddenly I saw the infamous

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 manage my environments with pyenv, the current python environment was 3.4.7. Let the script from the question be tst.py, I run

$ python tst.py 
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 make sure the wxpython is installed with brew

$ brew info wxpython
wxpython: stable 4.1.1 (bottled)
Python bindings for wxWidgets
https://www.wxpython.org/
/usr/local/Cellar/wxpython/4.1.1 (1,227 files, 91.8MB) *
  Poured from bottle on 2021-03-04 at 14:52:18
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/wxpython.rb
License: LGPL-2.0-or-later with WxWindows-exception-3.1
==> Dependencies
Required: freetype ✔, jpeg ✔, libpng ✔, libtiff ✔, numpy ✔, [email protected] ✔
==> Analytics
install: 643 (30 days), 2,701 (90 days), 12,659 (365 days)
install-on-request: 604 (30 days), 2,523 (90 days), 9,914 (365 days)
build-error: 0 (30 days)

The wxPython wiki suggests

The reason is because some 3rd party tools like PyInstaller might require CPython installation be built with --enable-framework. What you need to do is run $ env PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install 3.x.x in your terminal.

so I re-installed 3.7.4 from scratch

$ pyenv uninstall 3.7.4
$ env PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install 3.7.4

and tried the script again

$ python tst.py 
2021-03-04 15:14:44.138 Python[18815:404240] ApplePersistenceIgnoreState: Existing state will not be touched. New state will be written to (null)

of course it does not completely resolves it but at least the annoying error is gone...


I ran into the same problem. In order to use anaconda's python executable and wxPython on a mac you will need to run "pythonw" (instead of "python"). This calls the python executable that is compatible with wxPython. But in order to get it to work on my mac I had to update my anaconda packages by running:

conda install anaconda #you might not need this if anaconda is up to date

followed by:

conda install wxPython

which installs the "pythonw" executable in the "//anaconda3/bin" directory (you might have anaconda installed somewhere else). Then I could run any "program_with_xwPython.py" that imports/contains and uses wx using "pythonw" as follows:

pythonw program_with_xwPython.py # Note: 'python program_with_xwPython.py' gives the error still, you need to run 'pythonw'

You can then also launch a python REPL (i.e the >>> prompt) that works with wxPython using:

pythonw # instead of 'python'

This let's you import wx and run wx apps with the python CLI. It's been working like a champ for me.