我在我的Mac上安装了Python 2.7作为Framework构建。我已经安装并确认wxPython适用于这个Python构建。但是当我使用virtualenv创建虚拟环境时,生成的Python可执行文件不是框架构建,或者至少不是根据wxPython:
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.
有任何想法吗?
另一种解决方案是将以下脚本添加到/ 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 "$@"
然后,每当你想运行GUI(ex wxPython)时,使用my_script main.py
(确保virtualenv处于活动状态)
同样的问题,pythonw
不作为框架提供。有一个workaround可用。我们正在使用它来使pythonw
作为Framework应用程序包提供:
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
在10.10.3,使用virtualenv(如果重要,通过pyenv),我做了一个brew install wxmac
。
我有我的应用程序脚本
import site
site.addsitedir("/usr/local/lib/python2.7/site-packages")
我使用这个包装器来运行我的脚本,名为'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 "$@"
用fwpy app.py
运行它
要回答问题:virtualenv中的Python可执行文件是执行主Python可执行文件的存根,并且不存在于可以自动识别包的路径中。这导致Foundation.NSBundle.mainBundle()
返回一个NSBundle
只是指向virtualenv中的bin/
目录,没有相关的Info.plist
因此没有相关的bundleIdentifier
;这使得无法使用某些API(例如,正如您所注意到的,Cocoa GUI API)。
我将这些变通方法打包成一个小工具,你可以pip install
进入你的virtualenv,然后运行:
它现在非常原始;你必须在你的virtualenv做pip install venvdotapp; venvdotapp
。你也可以做import venvdotapp; venvdotapp.require_bundle()
,如果你正在编写一些代码来重新审视你的venv有捆绑。
加
export PYTHONHOME=$VIRTUAL_ENV
alias python=/Library/Frameworks/Python.framework/Versions/3.6/bin/python3 # set path to ur python
在venv激活脚本的最后