I\'ve installed Python 2.7 as a Framework build on my Mac. I\'ve installed and confirmed that wxPython works with this Python build. But when I create a virtual environment
Another solution is to add the following script to /Path/To/VirtualEnv/bin:
ENV=`python -c "import sys; print sys.prefix"`
PYTHON=`python -c "import sys; print sys.real_prefix"`/bin/python
export PYTHONHOME=$ENV
exec $PYTHON "$@"
Then, whenever you want to run GUI (ex wxPython) use my_script main.py
(make sure that virtualenv is active)
add
export PYTHONHOME=$VIRTUAL_ENV
alias python=/Library/Frameworks/Python.framework/Versions/3.6/bin/python3 # set path to ur python
in the end of venv activate script
To answer the question as asked: the Python executable in the virtualenv is a stub that executes the main Python executable, and is not present at a path where a bundle can be automatically recognized. This results in Foundation.NSBundle.mainBundle()
returning an NSBundle
just pointing at the bin/
directory in the virtualenv, with no associated Info.plist
and therefore no associated bundleIdentifier
; this makes it impossible to use certain APIs (such as, as you've noticed, the Cocoa GUI APIs).
I packaged up these workarounds into a small tool you can pip install
into your virtualenv and then run:
It's pretty primitive right now; you have to just do pip install venvdotapp; venvdotapp
in your virtualenv. You can also do import venvdotapp; venvdotapp.require_bundle()
if you're writing some code that reqiures your venv have a bundle.
On 10.10.3, using a virtualenv (via pyenv if it matters), I did a brew install wxmac
.
I have this atop of my application script
import site
site.addsitedir("/usr/local/lib/python2.7/site-packages")
And I use this wrapper to run my script, called 'app.py'
#!/bin/bash
# what real Python executable to use
PYVER=2.7
PYTHON=/System/Library/Frameworks/Python.framework/Versions/$PYVER/bin/python$PYVER
# pythonw is key here!
PYTHON="pythonw"
# now run Python with the virtualenv set as Python's HOME
export PYTHONHOME=$VIRTUAL_ENV
exec $PYTHON "$@"
Run it with fwpy app.py
Same issue here, pythonw
is not available as a Framework. There is a workaround available. We're using it like this to make pythonw
available as a Framework app bundle:
curl -O https://raw.githubusercontent.com/gldnspud/virtualenv-pythonw-osx/master/install_pythonw.py
curl -O https://raw.githubusercontent.com/gldnspud/virtualenv-pythonw-osx/master/pythonw.c
python install_pythonw.py `which python`/../..
rm install_pythonw.py pythonw.c