简体   繁体   English

Virtualenv下的结构:如何获取系统的Python库路径(不是virtualenv Python的库路径)

[英]Fabric under Virtualenv: How to obtain the system's Python library path (not the library path of the virtualenv Python)

I'm starting using Fabric as simple deployment tool for a Python project, but there's a task that needs to know where's the system Python library path, to correctly setup some packages symlinks. 我开始使用Fabric作为Python项目的简单部署工具,但是有一项任务需要知道系统Python库路径在哪里,以正确设置一些软件包符号链接。

So the question is: How, in my fabfile, can I obtain the system Python path? 因此问题是:如何在fabfile中获取系统Python路径? For instance, this Python code return the correct library path: 例如,以下Python代码返回正确的库路径:

# Without virtualenv activated
>>> from distutils.sysconfig import get_python_lib;
>>> print get_python_lib()
'/usr/lib/python2.7/dist-packages'

# With virtualenv activated
>>> from distutils.sysconfig import get_python_lib;
>>> print get_python_lib()
'/home/mandx/dev/my_project/env/lib/python2.7/site-packages'

In my case, I need to know both, when running a task in Fabric (and with virtualenv active). 就我而言,在Fabric中运行任务时(以及在启用virtualenv的情况下),我需要同时了解两者。

I'll explain my need for this: I want to use WeasyPrint to generate PDFs by simply passing HTML and CSS content to a constructor and calling a render method. 我将对此解释:我想使用WeasyPrint通过将HTML和CSS内容简单地传递给构造函数并调用render方法来生成PDF。 This flexibility is good because a can now leverage the PDF layout to a designer with CSS knowledge, and WeasyPrint strives CSS3 compatibility. 这种灵活性非常好,因为a现在可以将PDF布局用于具有CSS知识的设计人员,并且WeasyPrint致力于CSS3兼容性。

And the trade-off is that it depends on some libraries that can not be installed with Pip (specifically Pango, GdkPixbuf, and Cairo). 而且要权衡的是,它取决于某些无法通过Pip安装的库(特别是Pango,GdkPixbuf和Cairo)。 So the solution I have for now is to have those installed system-wide and symlink them to the project's virtualenv. 因此,我现在的解决方案是在系统范围内安装它们,并将它们符号链接到项目的virtualenv。

I don't like much the idea of using the virtualenv's --system-site-packages option, but I would like to hear why it would be better than my idea of symlinking. 我不太喜欢使用virtualenv的--system-site-packages选项的想法,但是我想听听为什么它比我的符号链接想法更好。

只需运行Fabric Shell脚本命令任务来询问操作系统的python:

/usr/bin/python -c 'from distutils.sysconfig import get_python_lib;print get_python_lib()'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM