如何正确安装PySide到可重定位的virtualenv?

时间:2015-10-02 13:03:48

标签: python virtualenv pyside

我正在将PySide安装到virtualenv:

sudo virtualenv --always-copy $VENV
source $VENV/bin/activate
sudo $VENV/bin/pip install pyside
sudo $VENV/bin/python $VENV/bin/pyside_postinstall.py -install
deactivate

virtualenv效果很好,我可以毫无问题地运行我的Python / PySide脚本:

$VENV/bin/python $SCRIPT

现在,我让我的virtualenv可重定位:

virtualenv --relocatable $VENV

我移动它:

mv $VENV $VENV_RELOCATED

...再次运行我的脚本:

source $VENV_RELOCATED/bin/activate
$VENV_RELOCATED/bin/python $SCRIPT

这一次,我收到了一个错误:

Traceback (most recent call last):
  File "script.py", line 1, in <module>
    from PySide import QtCore, QtGui, QtUiTools
ImportError: dlopen(/absolute/path/to/relocated_venv/lib/python2.7/site-packages/PySide/QtCore.so, 2): Library not loaded: @rpath/libpyside-python2.7.1.2.dylib
  Referenced from: /absolute/path/to/relocated_venv/lib/python2.7/site-packages/PySide/QtCore.so
  Reason: image not found

这些是脚本的内容:

from PySide import QtCore, QtGui, QtUiTools

请注意:错误消息中打印两次的绝对路径是$ VENV_RELOCATED

这一切都在OS X 10.11.0上,包含Python 2.7.10和PySide 1.2.2。

问题:我应该如何正确地将PySide安装到可重定位的virtualenv中?

1 个答案:

答案 0 :(得分:0)

事实证明,下一个PySide版本(可能在发布时标记为“1.2.3”)已修复:https://github.com/PySide/PySide/issues/129

然而,我还使得PySide 1.2.2在OS X 10.11(El Capitan)上的可重定位的virtualenv中工作。为此,我修改了$ VIRTUALENV / bin / pyside_postinstall.py脚本中的localize_libpaths()函数。

修补程序详细信息:如果在执行脚本时检测到virtualenv,则不会使用@rpath。相反,它将使用@executable_path并相对于virtualenv中的python二进制文件进行搜索。由于PySide模块总是安装在virtualenv中的相同位置,我认为这应该适用于所有场景。

def localize_libpaths(libpath, local_libs, enc_path=None):
    """ Set rpaths and install names to load local dynamic libs at run time

    Use ``install_name_tool`` to set relative install names in `libpath` (as
    named in `local_libs` to be relative to `enc_path`.  The default for
    `enc_path` is the directory containing `libpath`.

    Parameters
    ----------
    libpath : str
        path to library for which to set install names and rpaths
    local_libs : sequence of str
        library (install) names that should be considered relative paths
    enc_path : str, optional
        path that does or will contain the `libpath` library, and to which the
        `local_libs` are relative.  Defaults to current directory containing
        `libpath`.
    """
    if enc_path is None:
        enc_path = abspath(dirname(libpath))
    install_names = osx_get_install_names(libpath)
    need_rpath = False
    for install_name in install_names:
        if install_name[0] in '/@':
            continue
        if hasattr(sys, 'real_prefix'):
            # virtualenv detected - use @executable_path
            back_tick('install_name_tool -change %s @executable_path/../lib/python2.7/site-packages/PySide/%s %s' %
               (install_name, install_name, libpath))
        else:
            # not a virtualenv - use @rpath
            back_tick('install_name_tool -change %s @rpath/%s %s' %
               (install_name, install_name, libpath))
            need_rpath = True
    if need_rpath and enc_path not in osx_get_rpaths(libpath):
        back_tick('install_name_tool -add_rpath %s %s' %
           (enc_path, libpath))

在Windows上,使用PySide重新定位virtualenv没有问题。但是,对于Linux,我还没有找到解决方案。我想等待PySide 1.2.3就是去这里的方式。