如何在virtualenv中安装wxPython

时间:2012-05-05 00:10:52

标签: python windows macos wxpython virtualenv

我在Mac OSX Lion机器上,我已经下载了wxPython-src-2.9.3.1.tar.bz2。然后我做了以下(注意:输出消息已被删除):

$ tar -xjf wxPython-src-2.9.3.1.tar.bz2
$ cd wxPython-src-2.9.3.1
$ mkdir bld
$ cd bld
$ source /path/to/myvirtualenv/bin/activate
(myvirtualenv)$ cross_compiling=yes
(myvirtualenv)$ export MACOSX_DEPLOYMENT_TARGET=10.6.7
(myvirtualenv)$ set arch_flags="-arch ppc64 "
(myvirtualenv)$ ../configure \
--with-mac --enable-monolithic --enable-threads --enable-unicode \
--enable-debug_flag --enable-debug \
--with-libpng --with-libjpeg --with-libtiff --enable-unicode \
--with-opengl --enable-graphics_ctx --with-odbc --enable-gui \
--with-macosx-sdk=/Developer/SDKs/MacOSX10.6.sdk --with-macosx-version-min=10.6 \
CFLAGS="$arch_flags" CXXFLAGS="$arch_flags" CPPFLAGS="$arch_flags" LDFLAGS="$arch_flags" OBJCFLAGS="$arch_flags" OBJCXXFLAGS="$arch_flags" --prefix=/path/to/myvirtualenv/
$ (myvirtualenv)make
$ (myvirtualenv)make install

之后,我收到了这条消息(所以我猜它成功了):

...
 ------------------------------------------------------

 The installation of wxWidgets is finished.  On certain
 platforms (e.g. Linux) you'll now have to run ldconfig
 if you installed a shared library and also modify the
 LD_LIBRARY_PATH (or equivalent) environment variable.

 wxWidgets comes with no guarantees and doesn't claim
 to be suitable for any purpose.

 Read the wxWindows Licence on licencing conditions.

 ------------------------------------------------------

然后把我送回了我的贝壳。但是,我似乎无法使用它

(myvirtualenv)$ python
>>> import wxversion
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named wxversion

如何在virtualenv中安装它?

6 个答案:

答案 0 :(得分:8)

对于其他人来说,这对我有用:

在Mac OSX上,我使用:

在Homebrew中安装了wxpython
brew install wxpython

转到virtualenv site-packages目录:

cd /venv/lib/python2.7/site-packages

然后链接wx.pth

ln -s /usr/local/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/wx.pth wx.pth

然后链接wx-3.0-osx_cocoa目录:

ln -s /usr/local/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/wx-3.0-osx_cocoa wx-3.0-osx_cocoa

答案 1 :(得分:3)

阅读完上述所有文章后,这才是真正的关键:

你需要将你的VE指向主要的python安装。

在我的系统上:

% ln /Library/Python/2.7/site-packages/wxredirect.pth ./default/lib/python2.7/site-packages/wxredirect.pth

答案 2 :(得分:2)

对于Windows,您可以使用cweston为OS X概述的相同方法,将其转换为对mklink的调用。通过执行以下操作,我成功地为旧的基于Python 2.7 / WxPython 2.8的应用程序创建了virtualenv:

使用安装程序安装WxPython。

找到安装了WxPython的site-packages目录。对我来说,

C:\Python27\Lib\site-packages

打开一个shell并切换到要添加WxPython的virtualenv中的site-packages目录,称它为WxApp

cmd
cd C:\Virtualenvs\WxApp\Lib\site-packages

然后创建链接:wx.pth和wxversion.py的硬链接,以及包含WxPython安装的目录的联结(我的是wx-2.8-msw-unicode):

mklink /h wx.pth C:\Python27\Lib\site-packages\wx.pth
mklink /h wxversion.py C:\Python27\Lib\site-packages\wxversion.py
mklink /j wx-2.8-msw-unicode C:\Python27\Lib\site-packages\wx-2.8-msw-unicode\

现在我可以访问wx模块:

C:\> C:\VirtualEnvs\WxApp\scripts\activate.bat
(WxApp) C:\>python
ActivePython 2.7.2.5 (ActiveState Software Inc.) based on Python 2.7.2 (default, Jun 24 2011, 12:21:10) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import wx
>>> wx.version()
'2.8.12.1 (msw-unicode)'
>>>

答案 3 :(得分:0)

我写了一个小脚本,修复了我的项目。认为分享可能会很好。

我有以下设置:

  • python和wxpython通过home-brew安装
  • 使用virtualenvwrapper管理虚拟环境

使用mkproject启动新项目后,我运行以下脚本来解决此问题。

#!/usr/bin/env bash

echo This script fixes issues with wxpython and virtualenv on mac os. >&2
echo It needs to be run inside a virtualenv created with virtualenvwrapper. >&2
echo It also assumes that wxpython was installed through homebrew. >&2
echo

PYTHON_FULL_VERSION=$(python --version 2>&1 | awk -F ' ' '{ print $2 }')
PYTHON_SHORT_VERSION=$(python -c 'import sys; print "%d.%d" % (sys.version_info[0], sys.version_info[1])')

echo detected python version: "$PYTHON_FULL_VERSION" >&2

[ -d "$VIRTUAL_ENV" ] || { echo "ERROR: First activate the virtualenvironment." >&2; exit 1; }

# PART 1: Add homebrew-installed wx to env's site-packages
HOMEBREW_SITE_PKG="/usr/local/lib/python${PYTHON_SHORT_VERSION}/site-packages"
VENV_SITE_PKG="${VIRTUAL_ENV}/lib/python${PYTHON_SHORT_VERSION}/site-packages"

ln -s "${HOMEBREW_SITE_PKG}/wx.pth" "$VENV_SITE_PKG"
ln -s "${HOMEBREW_SITE_PKG}/wx-3.0-osx_cocoa" "$VENV_SITE_PKG"

# PART 2: At activation of venv we set PYTHONHOME
# NB This needs virtualenvwrapper to work
echo 'export PYTHONHOME="$VIRTUAL_ENV"' >> "${VIRTUAL_ENV}/bin/postactivate"

# PART 3: link the pythonw executable in the virtualenv
PYTHONW="/usr/local/Cellar/python/${PYTHON_FULL_VERSION}/bin/pythonw"
ln -s "$PYTHONW" "${VIRTUAL_ENV}/bin/pythonw"

echo "You should now re-activate the environment."

答案 4 :(得分:0)

我相信最干净的就是尽可能让你的系统python尽可能干净。你在其中添加越多的随机内容越多,你就会遇到一些模糊不清的问题。

BUT

因为它是系统范围的brew安装,这是wxPython的最简单选择,由于某种原因,它似乎没有适当的python打包/ pip集成。在这种情况下,它将进入您的系统范围的站点包。

virtualenv支持以只读方式将系统范围的站点包链接到virtualenv。确保使用 - system-site-packages 开关创建virtualenv。这将使它对底层的python安装透明。

我希望这有助于人们在将来遇到这种情况。

答案 5 :(得分:0)

那是因为当您安装python版本时,您必须要求使用--enable-framework构建 CPython 安装。 因此,在Mac上,您必须运行以下命令:

$ env PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install 3.x.x