来到了新公司上班,首先就是得把本身的环境给搭起来。知乎使用了buildout做为python项目的构建工具。html
那么什么是 buildout ?python
buildout的是一款自动化构建工具。
由Zope
团队开发维护。包名为zc.buildout
。session
buildout
能够为应用构建独立的依赖环境。相似于virtualenv
,但两者还有不一样。
粗略地讲,buildout
支持的功能更多更便于自动化并且具体定位有所不一样。app
首先咱们创建一个 python 独立环境的沙盒,不论是 virtualenv 仍是 miniconda 都行。配置好沙盒以后安装 buildout :工具
pip install zc.buildout
建立一个配置文件 buildout.cfg。配置文件是整个构建过程的核心,这里我直接上一个比较全的配置挨个说明字段含义 :ui
[buildout] develop = . index = mirror地址 newest = false update-versions-file = versions.cfg extends = versions.cfg relative-paths = true show-picked-versions = true versions = versions parts = app test gen-thrift [app] recipe = zc.recipe.egg interpreter = python eggs = zticket gunicorn setuptools tzone.cli ipython miller2 flake8 extra-paths = ${buildout:directory}/gen-py [gen-thrift] recipe = plone.recipe.command command = 我能够随便使用一个命令 update-command = ${:command} [test] recipe = pbp.recipe.noserunner eggs = ${app:eggs} coverage boring mock defaults = -vd --with-coverage --with-xunit --cover-xml --cover-package=zticket --boring extra-paths = ${buildout:directory}/gen-py
这里注意一下变量替代
${} 这个语法
${:command} 这个表明的是当前应用下的command里面的值
${app:eggs} 这个表明的是app应用下eggs的值
能够看到,配置使用的是 INI FILE 语法,详情能够查看refer里面的描述和写法,这里只是提一下。this
[buildout]: 这是一个必须的 session 块。url
develop: 用来管理开发库的一个东西,通常不须要配置。这里配置`.` 符号,在执行 buildout 命令的时候生成的 develop-eggs 文件夹将会是一个空的。spa
newest: 这个参数默认是 true,若是是 true,那么会在 buildout 的时候老是检查最新版本,若是为 false 只有在包不知足需求的时候才会去更新版本。插件
update-versions-file: 指定一个包依赖的更新文件。通常使用 versions.cfg 来保存须要的依赖。
extended: 扩展配置的指定。
relative-path: 启用相对路径。
show-picked-versions: 这个字段默认是 false 的,若是是 true,当 buildout 在找到一个最新的发布版并且知足 requirement 申明的时候,将会从新写一条配置进versions.cfg文件里,也就是说 update-versions-file 配置的文件中。相似于这种格式:
# Required by: # opentracing==1.0rc3 futures = 3.0.5
versions: 默认就是 versions 更详细能够参考这里。http://docs.buildout.org/en/latest/getting-started.html#pinned-versions。
到这里为止,buildout 的基础配置就结束了。下面的每一个 section 能够理解为一个另外的app。由上面配置的 parts 申明的 app section 名字。
下面介绍各 parts 里面申明的块里面都作了什么。
app section 下面:
recipes: 首先每一个块里面都必须包含一个 recipe 的项目,这个项目用来神明本身使用了什么工具。各类其余的包在 pypi 上都能下载到,各有不一样的用处。也许这里能够将它理解成可使用不一样的插件。最常使用的是 zc.recipe.egg 这个 recipe 了,他能够被用来安装各类各样的包,而且打包成 egg 。
interpreter: 会建立一个包含eggs和依赖关系的环境在bin目录下面。
eggs: 是一个list被用来安装一个或者多个setuptools的依赖eggs。
extra-paths: 这个能够理解成,须要编译出应用以后应该被加入sys.path的路径。
gen-thrift section 下面:
使用了一个 plone.recipe.command 的 recipe,这个 recipe 的功能参见 https://pypi.python.org/pypi/plone.recipe.command?,简单来讲就是一个能够提供在 build 的时候执行一个命令。
command: 在 build 的时候须要执行的命令。
update-command: 在 build 更新的时候须要执行的命令。
test section 下面:
使用的 recipe pbp.recipe.noserunner 查看 https://pypi.python.org/pypi/pbp.recipe.noserunner/0.2.6。
eggs: 和上面的创建依赖包类似,指定相关 eggs 。
default: 指定一个testrunner的默认选项。
extra-paths: 一样为应用中增长相关 sys.path。
在配置好相关设置以后,就能够开始愉快 build 了。build 以后能够发现,全部依赖包都被安装进了文件夹 eggs 中。just like this:
若是你足够仔细,能够发现这些都是文件夹。可是sys.path里面直接add他们的路径,就能够直接 import 他们。
sys.path like this:
能够看到 buildout 帮助咱们完成了一个封闭环境而且自动实现包管理。有点 pip+virtualenv/conda 的感受。并且这个环境能够在编译完成以后任意在一样 python 解释器环境下移动。
另外提一点,在下载环境的时候速度也比较重要,咱们能够从新设置全局 pip 和 easy_install 源,来加速包安装。
经过环境变量修改:
export PIP_INDEX_URL=https://mirror.in.zhihu.com/simple
经过配置文件修改:
~/.pip/pip.conf [global] extra-index-url = mirror ~/.pydistutils.cfg [easy_install] index_url = mirror
另外,其余的想到以后再补充,以上。
Reference:
http://yabin.me/2017/07/25/buildout构建工具/ buildout构建工具
https://www-archive.mozilla.org/projects/cck/docs/WizardMachine/syntax.html INI file syntax
http://docs.buildout.org/en/latest/reference.html buildout官方文档