Dec 16, 2019 Find the appropriate download link in the upgrade instructions for each macOS version: macOS Catalina, macOS Mojave, and macOS High Sierra download directly to your Applications folder as an app named Install macOS Catalina, Install macOS Mojave, or Install macOS High Sierra. If the installer opens after downloading, quit it without continuing installation.
Introduction
The virtualenv tool is a great way to easily set up a Python environment that is isolated from the other Python installs that may be on your computer. These isolated environments enable the possibility of installing a custom set of 3rd party packages that may be quite different than what is installed in your global Python, without the installed packages cluttering up the import path or conflicting with other versions of the same package in the global Python. For more details on virtualenv please visit their PyPI page at http://pypi.python.org/pypi/virtualenv.
Despite all of virtualenv's goodness, it does have a problem that impacts those who want to use it with wxPython on the Mac. In a nutshell the problem is that wxPython requires a framework build of Python so it is given permission to access the display and create GUI objects, and although the Python binary used by virtualenv is from the framework, the rest of the environment is not the proper structure to be recognized as a framework by the system. So when you try to create a wx.App in this environment wxPython detects that it doesn't have access to the display and it gives you an error.
There is another wxPython virtualenv complication due to the complex nature of how wxPython is installed that makes it difficult (or perhaps impossible) to 'install' wxPython into the virtualenv like you would for other packages. However there is a very simple workaround that I'll show below.
If you are getting the error that '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.' 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.
Before starting I should give a bit of credit to others that have worked on solving this problem already. I read Batok's page and that gave me the hints I needed to be able to come up with the solution that I present here. The reason I didn't just use his approach is that it removes some of the isolation as the virtualenv ends up having access to the eggs and etc. installed in the global Python and this somewhat defeats the purpose of having the virtualenv.
How to use wxPython with virtualenv on Mac OSX
I'll assume that you already have Python installed (I'm using 2.7.1 installed from Python.org's x86-64/i386 installer) and that you've also installed virtualenv for that Python, and that you are familiar with virtualenv. If not then see virtualenv's documentation for how to proceed.
The first thing we will do is to create the virtual environment for this demonstration. This is how I do it:
If you look in the ./test1 folder you'll find a bin dir with a python executable in it, as well as a lib dir populated with a copy of the Python standard library and also a couple packages installed in site-packages. That was easy, eh? If you run that instance of Python and look at the sys.path you'll see that the path is probably very different than what you get from doing the same thing with your global Python, especially if you've installed lots of eggs.
One of the extra goodies installed in the environment's bin dir is a script that fiddles with the $PATH such that the environment's version of python and the other executables there will be found first. It's a nice convenience, but not strictly necessary for using the environment.
Notice that I run the command with an extra '.' (dot) at the beginning of the command line. This causes the script to run in the current shell instead of spawning a new shell for it. That way the settings it makes persist in the current shell after the script completes. Also notice that the prompt string has changed to show the name of the environment. There is also a deactivate command that can be used to restore things as they were before.
When you use the environment's Python to build and install a Python module or package, or if you use the environment's easy_install or pip commands to grab something from PyPI then it is installed into the environment's site-packages folder. Let's try installing py2app just to see how it works (NOTE: I clipped out some warnings that you can ignore):
Ok, the next step is to make wxPython available to this virtual environment. As mentioned above wxPython's installation is complex enough that it would be difficult to install in the environment in the normal manner. However wxPython is already installed in a way that allows it to be somewhat isolated from different wxPython versions and also to be shared by different versions of Python, such as Apple's Python and the Python from python.org, so we can easily use that installation structure in the virtual environment as well. All it takes is adding a .pth file in the environment's site-packages that tells it how to find wxPython, and to make it even simpler we'll just link to the file in the global Python's site-packages folder. That way when wxPython is upgraded then the virtual environment will see the updated version as well.
At this point it may appear that everything is working and that we're finished. However as mentioned above this environment is not recognized as a framework or application bundle by the system and so wxPython applications can not initialize.
All we need to do to fix this is to use the Python framework's python executable, but set it up in such a way that the virtual environment's library and site-packages will be used instead of those in the framework. It turns out that somebody has used Guido's Time Machine and implemented this feature for us in the form of the PYTHONHOME environment variable. By setting that to the virtualenv's location you can cause Python at runtime to use a different location as its install prefix, including where it looks for the site.py file which is where the virtualenv tool implements some of it's magic. I came up with this little script to help automate things for me:
All you need to do is copy this script into in a file in your environment's bin folder, (I called mine fwpy for FrameWork PYthon) and give it executable permissions. Then when you need to run some wxPython application in your virtual environment just use your copy of this script instead of 'python' and you should be all set.
Another possible solution would be to modify the activate script created by virtualenv to also set the PYTHONHOME value in the environment, and then copy the framework's python binary into the virtualenv's bin dir, overwriting the python that is already there. That way you can still use 'python' instead of whatever you named your wrapper script, but you would also have to ensure that you always use the activate script and never run the virtualenv's python without it.
Have fun!
RobinDunn
Comments
FWIW, I found it useful to put this in my activate script, to implement the suggestion in the last paragraph:
I also linked the framework python into the virtualenv bin, rather than copy it, so it is clear where it is actually coming from. It seems to work OK.
~~MartinGregory
2015-09-20: I found that these instructions no longer quite worked, especially if you use the Python installed by the Homebrew package manager. I wrote a script to invoke RunSnakeRun that's available at my blog: http://www.georgevreilly.com/blog/2015/09/20/RunSnakeRun-WxPython-Brew-Virtualenv.html
Change WXPYTHON_APP to launch some other wxPython app. Use the correct value of FRAMEWORK_PYTHON for your system. This works for me with OS X 10.10.5, wxPython 3.0.2, and the Brew installation of Python 2.7.10.
--GeorgeVReilly
wxPython GUI The wxPython module can be used to create a graphical application (GUI) that looks like a native application on any operating system including Windows, Mac OS X and Linux.The official wxPython site has several screenshots and downloads for these platforms. wxPython is based on wxWidgets.
Related course: Creating GUI Applications with wxPython
Install wxPython
First download and install WxPython, the Python bindings for wxWidgets.Then install a GUI creator called wxglade:
Using a GUI builder such as wxGlade will save you a lot of time, regardless of the GUI library you use. You can easily make complex graphical interfaces because you can simply drag and drop.
Creating our first GUI with Python and wxWidgets:
![Install Wxpython Mac Os Install Wxpython Mac Os](/uploads/1/2/5/8/125825897/755976979.jpeg)
Start wxglade. You will see its user interface:
wxglade
Press on tiny window on the top left, below the file icon.
wxgladePress OK. An empty window will now appear. Press on the tiny [OK] button in the wxGlade panel and press on the frame. The button will now appear. Press on Application in the tree window.
wxgladeSet the output file in the wxproperties window.
wxgladeIf you look at the window note you can select multiple programming languages and two versions of wxWidgets. Select Python and wxWidgets 2.8. Finally press Generate code. (Do NOT name the file wx.py because the import needs wx, save it as window.py or something else).
Running wxglade code:
Run:
And a window with a button will appear. Pressing the button will not do anything. To start a function when pressing the button, we need to define a so called Callback. This can be as simple as:
Finally we bind the button to the callback function using:
Pressing the button will now write a message to the command line. Instead of the boring command line message, we want to show a message box. This can be done using this command:
wxPython example code
The full code below: