virtualenv that can find relocated libraires (like mysqlclient lib for MySQLdb)

回眸只為那壹抹淺笑 提交于 2019-12-19 00:57:10

问题


I would like to be able to share a virtualenv that is self contained, i.e. insure that all scripts installed are able to run directly without needing to set anything. E.g. I install a script in my virtualenv that uses MySQL-python. Unfortunately importing MySQLdb looks for a shared library (libmysqlclient.so) that was moved elsewhere than the standard directories on my system. Is there a way to guarantee that my virtualenv will find the library every time someone uses it?


回答1:


I know this question is a bit old, but I'd love to share my solution for the lack of finding it via Google:

In the deactivate () function, add the following lines:

    if ! [ -z ${_OLD_LD_LIBRARY_PATH+x} ] ; then
        LD_LIBRARY_PATH="$_OLD_LD_LIBRARY_PATH"
        export LD_LIBRARY_PATH
        unset _OLD_LD_LIBRARY_PATH
    fi

Note that I'm using a negated -z expression - the existing reset scripts for PATH and PYTHONHOME use -n, which incorrectly evaluates to false if _OLD_LD_LIBRARY_PATH is set to an empty string.

Then, below the deactivate () function, add the following section:

_OLD_LD_LIBRARY_PATH="$LD_LIBRARY_PATH"
LD_LIBRARY_PATH="$VIRTUAL_ENV/lib:$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH



回答2:


This is maybe not the best solution, but you can hack the virtualenv active script to set some environment variable (this is a script that we will need to use anyway). In the installation script of the virtualenv, I added:

echo 'export LD_LIBRARY_PATH=/path/to/lib:$LD_LIBRARY_PATH' >> /path/to/virtualenv/activate

Now every time someone uses my virtualenv, it also includes the LD_LIBRARY_PATH to find the mysql libraries.

Drawbacks I can see:

  • Don't work nicely with virtualenv's deactivate

  • Won't help if you want to distribute your package with the standard distutils setup.py.



来源:https://stackoverflow.com/questions/22771204/virtualenv-that-can-find-relocated-libraires-like-mysqlclient-lib-for-mysqldb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!