构建和 Virtualenv
我正在尝试将 buildout 和 virtualenv 结合起来,在 python 中设置一个独立的开发环境,以允许进行可重复的构建。
有一个构建方法,可以让你将 virtualenv 集成到构建中:
tl.buildout_virtual_python
这样我的 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
这会将两个可执行文件部署到 ./bin/ 中:
vp
script
当我执行 vp 时,我会得到一个交互式的、独立的 python 对话框,如下所示预期的(无法从系统加载任何包)。 我现在期望的是,如果我运行
./bin/script
,将使用隔离的 python 解释器。但事实并非如此,它并不像“vp”那样孤立(意味着我可以从系统级别导入库)。但是我可以运行:
./bin/vp ./bin/script
这将按照我的意愿在隔离环境中运行脚本。但必须有一种方法来指定这一点,而不需要链接命令,否则构建只能解决我希望的一半问题:)
感谢您的帮助! 帕特里克
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.
There is a recipe for buildout that let's you integrate virtualenv into buildout:
tl.buildout_virtual_python
With this my buildout.cfg looks like 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
This will deploy two executables into ./bin/:
vp
script
When I execute vp, I get an interactive, isolated python dialog, as expected (can't load any packages from the system).
What I would expect now, is that if I run
./bin/script
that the isolated python interpreter is used. But it doesn't, it's not isolated as "vp" is (meaning I can import libraries from system level). 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 :)
Thanks for your help!
Patrick
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不需要 virtualenv:buildout 已经提供了一个隔离的环境,就像 virtualenv 一样。
例如,查看 bin 目录中 buildout 生成的文件。他们会有这样的东西:
所以 sys.path 被完全替换为构建想要在路径上拥有的内容:与 virtualenv 相同的隔离方法。
You don't need virtualenv: buildout already provides an isolated environment, just like virtualenv.
As an example, look at files buildout generates in the bin directory. They'll have something like:
So the
sys.path
gets completely replaced with what buildout wants to have on the path: the same isolation method as virtualenv.zc.buildout 2.0及更高版本不再提供隔离环境。
但virtualenv 1.9及更高版本提供了完全隔离(包括不安装setuptools)。
因此,在完全受控环境中进行构建的最简单方法是运行以下步骤(此处即针对 Python 2.7):
前提条件:
bootstrap.py
必须是与您正在使用的构建版本。您可以在http://downloads.buildout.org/2/找到最新信息如果您的构建中有任何版本引脚,请确保它们不会将构建本身或配方/扩展引脚固定到与 zc.buildout 2 或更高版本不兼容的版本。
zc.buildout 2.0 and later does not provide the isolated environment anymore.
But virtualenv 1.9 and later provides complete isolation (including to not install setuptools).
Thus the easiest way to get a buildout in a complete controlled environment is to run the following steps (here i.e. for Python 2.7):
Preconditions:
bootstrap.py
has to be a recent one matching the buildout version you are using. You'll find the latest at 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.
在 ubuntu 服务器上使用 bootstrap 运行 buildout 时遇到问题,从那时起我一起使用 virtualenv 和 buildout 。只需创建 virtualenv 并在其中安装 buildout 即可。这样,只需将 virtualenv 安装到系统中(理论上1)。
还告诉 buildout 将其脚本放入 virtual/bin/ 目录,这样脚本就会出现在
$PATH
上。1:在实践中,您可能需要将需要编译的内容添加到需要编译的系统级别。鸡蛋比如mysql或者memcache。
Had issue running buildout using bootstrap on ubuntu server, from then I use virtualenv and buildout together. Simply create virualenv and install buildout in it. This way only virtualenv has to be installed into system (in theory1).
Also tell buildout to put its scripts in to virtual/bin/ directory, that way scripts appear on
$PATH
.1: In practice you probably will need to eggs what require compilation to system level that require compilation. Eggs like mysql or memcache.
我以前从未使用过该配方,但我要尝试的第一件事是:
如果这不起作用,您通常可以在文本编辑器中打开脚本(在本例中为 vp 和 script)并查看 Python 路径他们正在使用的。如果您使用的是 Windows,通常会有一个名为
-script.py
的文件。在本例中,这将是 vp-script.py 和 script-script.py。I've never used that recipe before, but the first thing I would try is this:
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. If you're on windows there will usually be a file called
<script_name>-script.py
. In this case, that would be vp-script.py and script-script.py.