19

I'm trying to use the Python GUI wx (installable via pip install wxPython) in the following minimal app:

import wx
app = wx.App()

Running this snippet returns the following:

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.

Does anyone know how to help wx gain "access to the screen", or what a "Framework build of Python" is? I'd be grateful for any help others can offer with these questions!

duhaime
  • 25,611
  • 17
  • 169
  • 224
  • I think this blogpost may have a soultion https://blurringexistence.net/wxpython-using-virtualenvwrapper-on-osx.html. I didn't get a chance to read the whole thing though, so I'm not entirely sure. – Christian Dean Jan 30 '18 at 21:44
  • Running my script with `pythonw` worked for me. – lanery Mar 30 '18 at 08:43
  • @lanery can you give me more detail how to run script in pythonw? – pill45 Sep 15 '18 at 14:16
  • @pill45 try running `which pythonw` (assuming you are on unix). If you get output, that means the pythonw binary is on your path, so you can use: `pythonw myscript.py`. If you install Anaconda on your machine, that should give you access to pythonw – duhaime Sep 15 '18 at 14:18
  • 1
    When I run pythonw in the virtualenv where I install wx, it shows no module named wx, what did I miss? – pill45 Sep 15 '18 at 14:26
  • @pill45 did you try the solution I posted below? Sounds like you need to install your module from within python, as I show below... – duhaime Sep 15 '18 at 14:43
  • Thanks. Looks like I have to go back to anaconda, which I just get rid off. – pill45 Sep 15 '18 at 14:49
  • It's a phenomenal resource for Python programmers. I wouldn't get rid of it if you have space to keep it. It makes compiling lots of tricky tools simple even on Windows – duhaime Sep 15 '18 at 15:06

7 Answers7

17

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

Nick Weimer
  • 497
  • 5
  • 7
11

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
duhaime
  • 25,611
  • 17
  • 169
  • 224
4

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.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
RickWe
  • 111
  • 1
  • 2
2

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 ✔, python@3.9 ✔
==> 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...

Dharman
  • 30,962
  • 25
  • 85
  • 135
Marek
  • 1,413
  • 2
  • 20
  • 36
2

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

Solved the problem for me with my pyenv installation on Mac

Okkervile
  • 19
  • 1
1

Official wxPython website has a solution:

If you're using conda:

>> conda install python.app # this will install latest pythonw

>> pythonw your_app.py

0

@duhaime

There is an installer for OSX on the official wxPython web-site.

Just download it and install.

You don't need to play with anaconda just to install and run such a simple application.

Igor
  • 5,620
  • 11
  • 51
  • 103
  • do you have a link? This is what I see on their downloads page: https://www.wxpython.org/pages/downloads/ If you are talking about https://sourceforge.net/projects/wxpython/files/wxPython/3.0.2.0/ the installers I tried simply didn't work -- did they work on your machine? – duhaime Feb 01 '18 at 12:27
  • @duhaime, yes, it did. I guess Robin re-did the website design and push the wxPython to the PyPI place. What problem(s) did you encounter trying to install from the SF? Did you report it to the wxPython ML? – Igor Feb 01 '18 at 15:49
  • I didn't record and can't recall the error I had, though I just downloaded and built the code and it came through fine (though I'm still not sure how I'd run the Python script with these application files). That said, I spent an hour helping a professor get old student code running; it's unlikely I'll use this library again... – duhaime Feb 01 '18 at 23:20
  • 1
    @duhaime, if it is the old code that was written with the old wxPython version there will be an effort to update it to the new version. And depending on the version it used to be written that effort might be significant. But this is no different from any other library out there - you still need to fix some stuff before everything compiles and runs smoothly. The only way to simplify is to upgrade gradually along with new releases, which you professor didn't do. And so the hour sounds like a very good time frame. – Igor Feb 02 '18 at 17:02