Use a single site package (as exception) for a virtualenv

喜夏-厌秋 提交于 2019-12-08 04:08:50

问题


In a virtualenv, how can I ignore the no-site-packages rule for a single package?

Some background: I use virtualenv for my deployments, but these take a lot longer since I have been using lxml. Compiling this takes up to 15 minutes each time I reinstall for a new virtualenv. Can I make some sort of an exception for lxml and use the global site package? Is there any safer/more reliable option than just copying it into the new virtualenv?


回答1:


Short answer: no, but you can do something else to solve the same problem.

The --no-site-packages option (which is now the default unless you specify --system-site-packages) controls whether or not some directories are added to sys.path. A given directory is either in or not, you can not discriminate only one package in that directory.

However, you can make a symbolic link to the package in the virtual environment’s site-packages directory. On my system:

ln -s /usr/lib/python2.7/site-packages/lxml ./env/lib/python2.7/site-packages

Or more generically (using the system’s python, not the environment’s):

ln -s $(python -c 'import lxml, os.path; print(os.path.dirname(lxml.__file__)') ./env/lib/python2.7/site-packages

If you’re on a system that does not support symbolic links, copying should also work but is more fragile when the system-wide lxml is updated.



来源:https://stackoverflow.com/questions/13703074/use-a-single-site-package-as-exception-for-a-virtualenv

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