[英]Buildout and Virtualenv
I am messing around with the combination of buildout and virtualenv to setup an isolated development environment in python that allows to do reproducible builds. 我正在搞乱buildout和virtualenv的组合来在python中设置一个孤立的开发环境,允许进行可重现的构建。
There is a recipe for buildout that let's you integrate virtualenv into buildout: 有一个buildout的配方,让你将virtualenv集成到buildout中:
tl.buildout_virtual_python
With this my buildout.cfg looks like this: 有了这个,我的buildout.cfg看起来像这样:
[buildout]
develop = .
parts = script
virtualpython
[virtualpython]
recipe = tl.buildout_virtual_python
headers = true
executable-name = vp
site-packages = false
[script]
recipe = zc.recipe.egg:scripts
eggs = foo
python = virtualpython
This will deploy two executables into ./bin/: 这将在./bin/中部署两个可执行文件:
vp
script
When I execute vp, I get an interactive, isolated python dialog, as expected (can't load any packages from the system). 当我执行vp时,我得到了一个交互式,孤立的python对话框,正如预期的那样(无法从系统加载任何包)。 What I would expect now, is that if I run 我现在所期待的是,如果我跑了
./bin/script
that the isolated python interpreter is used. 使用了孤立的python解释器。 But it doesn't, it's not isolated as "vp" is (meaning I can import libraries from system level). 但它没有,它不是孤立的“vp”(意思是我可以从系统级别导入库)。 However I can run: 但是我可以运行:
./bin/vp ./bin/script
Which will run the script in an isolated environment as I wished. 哪个会在我希望的孤立环境中运行脚本。 But there must be a way to specify this to do so without chaining commands otherwise buildout only solves half of the problems I hoped :) 但是必须有一种方法来指定这样做而不用链接命令,否则buildout只能解决我希望的一半问题:)
Thanks for your help! 谢谢你的帮助! Patrick 帕特里克
You don't need virtualenv: buildout already provides an isolated environment, just like virtualenv. 你不需要virtualenv:buildout已经提供了一个孤立的环境,就像virtualenv一样。
As an example, look at files buildout generates in the bin directory. 例如,查看bin目录中生成的文件buildout。 They'll have something like: 他们会有类似的东西:
import sys
sys.path[0:0] = [
'/some/thing1.egg',
# and other things
]
So the sys.path
gets completely replaced with what buildout wants to have on the path: the same isolation method as virtualenv. 因此, sys.path
完全被buildout想要在路径上取代:与virtualenv相同的隔离方法。
zc.buildout 2.0 and later does not provide the isolated environment anymore . zc.buildout 2.0及更高版本不再提供隔离环境 。
But virtualenv 1.9 and later provides complete isolation (including to not install setuptools). 但virtualenv 1.9及更高版本提供了完全隔离(包括不安装setuptools)。
Thus the easiest way to get a buildout in a complete controlled environment is to run the following steps (here ie for Python 2.7): 因此,在完全受控环境中获取构建的最简单方法是运行以下步骤(此处即对于Python 2.7):
cd /path/to/buildout
rm ./bin/python
/path/to/virtualenv-2.7 --no-setuptools --no-site-packages --clear .
./bin/python2.7 bootstrap.py
./bin/buildout
Preconditions: 前提条件:
bootstrap.py
has to be a recent one matching the buildout version you are using. bootstrap.py
必须是与您正在使用的buildout版本匹配的最新版本。 You'll find the latest at http://downloads.buildout.org/2/ 您可以在http://downloads.buildout.org/2/找到最新信息。
if there are any version pins in your buildout, ensure they do not pin buildout itself or recipes/ extensions to versions not compatible with zc.buildout 2 or later. 如果您的buildout中有任何版本引脚,请确保它们不会自行构建或将配方/扩展插入与zc.buildout 2或更高版本不兼容的版本。
Had issue running buildout using bootstrap on ubuntu server, from then I use virtualenv and buildout together. 如果在ubuntu服务器上使用bootstrap运行buildout问题,那么我将使用virtualenv和buildout。 Simply create virualenv and install buildout in it. 只需创建virualenv并在其中安装buildout。 This way only virtualenv has to be installed into system (in theory 1 ). 这种方式只需要将virtualenv安装到系统中(理论上1 )。
$ virtualenv [options_you_might_need] virtual
$ source virtual/bin/activate
$ pip install zc.buildout
$ buildout -c <buildout.cfg>
Also tell buildout to put its scripts in to virtual/bin/ directory, that way scripts appear on $PATH
. 还要告诉buildout将其脚本放入virtual / bin /目录,这样脚本就会出现在$PATH
。
[buildout]
bin-directory = ${buildout:directory}/virtual/bin
...
1: In practice you probably will need to eggs what require compilation to system level that require compilation. 1:在实践中,你可能需要鸡蛋,需要编译到系统级别,需要编译。 Eggs like mysql or memcache. 鸡蛋喜欢mysql或memcache。
I've never used that recipe before, but the first thing I would try is this: 我之前从未使用过这个食谱,但我要尝试的第一件事是:
[buildout]
develop = .
parts = script
virtualpython
[virtualpython]
recipe = tl.buildout_virtual_python
headers = true
executable-name = vp
site-packages = false
[script]
recipe = zc.recipe.egg:scripts
eggs = foo
python = virtualpython
interpreter = vp
If that doesn't work, you can usually open up the scripts (in this case vp and script) in a text editor and see the Python paths that they're using. 如果这不起作用,您通常可以在文本编辑器中打开脚本(在本例中为vp和脚本),并查看他们正在使用的Python路径。 If you're on windows there will usually be a file called <script_name>-script.py
. 如果你在Windows上,通常会有一个名为<script_name>-script.py
的文件。 In this case, that would be vp-script.py and script-script.py. 在这种情况下,那将是vp-script.py和script-script.py。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.