01.python.开发最佳实践
TRANSCRIPT
![Page 1: 01.python.开发最佳实践](https://reader033.vdocuments.pub/reader033/viewer/2022051112/55a0e6901a28ab10248b473b/html5/thumbnails/1.jpg)
© 2008 绿盟科技www.nsfocus.comnsfocus.com © 2008 绿盟科技www.nsfocus.comnsfocus.com
Python 开发最佳实践
密级:内部使用
—— Python 开发环境包管理
![Page 2: 01.python.开发最佳实践](https://reader033.vdocuments.pub/reader033/viewer/2022051112/55a0e6901a28ab10248b473b/html5/thumbnails/2.jpg)
好的开发环境意味着什么?
![Page 3: 01.python.开发最佳实践](https://reader033.vdocuments.pub/reader033/viewer/2022051112/55a0e6901a28ab10248b473b/html5/thumbnails/3.jpg)
![Page 4: 01.python.开发最佳实践](https://reader033.vdocuments.pub/reader033/viewer/2022051112/55a0e6901a28ab10248b473b/html5/thumbnails/4.jpg)
• Python 社区是一个变化非常快的社区– 知识和工具都需要及时更新,跟进社区的变化
• Python 开发的最佳实践(推荐阅读)– The Hitchhiker’s Guide to Python!
• 英文版:http://docs.python-guide.org/en/latest/
• Python 自身的包管理工具(推荐阅读)– Python 套件管理程序简介
• http://t.cn/zYdfQqV
– Python 的虚拟环境及多版本开发• http://t.cn/zjOzu0y
– Hitchhiker’s Guide:• Linux:http://t.cn/zYdfQqI
• Windows:http://t.cn/zYdfQqf
![Page 5: 01.python.开发最佳实践](https://reader033.vdocuments.pub/reader033/viewer/2022051112/55a0e6901a28ab10248b473b/html5/thumbnails/5.jpg)
Python 的第三方包管理
pip + virtualenv + virtualenvwrapper 基本操作
![Page 6: 01.python.开发最佳实践](https://reader033.vdocuments.pub/reader033/viewer/2022051112/55a0e6901a28ab10248b473b/html5/thumbnails/6.jpg)
• 知乎上的一个帖子 …… 神器?!
![Page 7: 01.python.开发最佳实践](https://reader033.vdocuments.pub/reader033/viewer/2022051112/55a0e6901a28ab10248b473b/html5/thumbnails/7.jpg)
• 主要工具(Python 包管理三大利器)
– pip
• Python 的包管理工具,原有 easy_install 不再维护
• http://www.pip-installer.org/en/latest/
– virtualenv
• Python 的环境管理工具,隔离 Python 不同环境的影响
• http://www.virtualenv.org/en/latest/
– virtualenvwrapper
• virutalenv 的增强工具包,实现了很多很方便的工具
• http://virtualenvwrapper.readthedocs.org/en/latest/
![Page 8: 01.python.开发最佳实践](https://reader033.vdocuments.pub/reader033/viewer/2022051112/55a0e6901a28ab10248b473b/html5/thumbnails/8.jpg)
pip + distribute
替代了setuptools + easy_install
![Page 9: 01.python.开发最佳实践](https://reader033.vdocuments.pub/reader033/viewer/2022051112/55a0e6901a28ab10248b473b/html5/thumbnails/9.jpg)
导出当前的第三方库
以 top 为例子
![Page 10: 01.python.开发最佳实践](https://reader033.vdocuments.pub/reader033/viewer/2022051112/55a0e6901a28ab10248b473b/html5/thumbnails/10.jpg)
• 导出当前的第三方库清单
– pip freeze > top.txt
• 导出当前的第三方库安装包(包括依赖库)
– source /usr/local/bin/virtualenvwrapper.sh(启用 virtualenvwrapper,非必须,可以写在 .bashrc 中)
– mkvirtualenv top.export(创建一个干净的环境用于导出文件)
– mkdir export.packages(准备一个导出文件用的目录)
– pip install -r top.txt -d export.packages(下载所有第三方库)
![Page 11: 01.python.开发最佳实践](https://reader033.vdocuments.pub/reader033/viewer/2022051112/55a0e6901a28ab10248b473b/html5/thumbnails/11.jpg)
从头创建一个开发环境
以 top 为例子
![Page 12: 01.python.开发最佳实践](https://reader033.vdocuments.pub/reader033/viewer/2022051112/55a0e6901a28ab10248b473b/html5/thumbnails/12.jpg)
• 启用 virtualenvwrapper(非必须,可以写在 .bashrc 中)
– source /usr/local/bin/virtualenvwrapper.sh
• 从 svn 获取最新代码(非必须)
– svn co https://svn.lab.intra.nsfocus.com/svn/top/trunk
• 创建一个 virutalenv 环境
– mkvirutalenv top.brandnew
• 从生产环境的 pip 文件中恢复
– pip install -r top.txt(top.txt 是从生产环境中导出的 python 包列表)
– pip freeze(查看当前安装的 python 库)
![Page 13: 01.python.开发最佳实践](https://reader033.vdocuments.pub/reader033/viewer/2022051112/55a0e6901a28ab10248b473b/html5/thumbnails/13.jpg)
复制/销毁一个现有的开发环境
以 top 为例子
![Page 14: 01.python.开发最佳实践](https://reader033.vdocuments.pub/reader033/viewer/2022051112/55a0e6901a28ab10248b473b/html5/thumbnails/14.jpg)
• 复制现有的 virtualenv
– cpvirtualenv top.brandnew top.sandbox(把 top.brandnew 这个环境复制到 top.sandbox)
– pip freeze
• 销毁现有的 virutalenv
– rmvirtualenv top.sandbox
![Page 15: 01.python.开发最佳实践](https://reader033.vdocuments.pub/reader033/viewer/2022051112/55a0e6901a28ab10248b473b/html5/thumbnails/15.jpg)
回头看看我们的第三方库管理
![Page 16: 01.python.开发最佳实践](https://reader033.vdocuments.pub/reader033/viewer/2022051112/55a0e6901a28ab10248b473b/html5/thumbnails/16.jpg)
要实现
一个功能搜寻
开源库
验证
是否可用选定
引入代码库开发 测试 上线
第三方库引入的示意图
![Page 17: 01.python.开发最佳实践](https://reader033.vdocuments.pub/reader033/viewer/2022051112/55a0e6901a28ab10248b473b/html5/thumbnails/17.jpg)
• 初始化环境(运维人员)– 标准的开发虚拟机模板(解决了系统库依赖)
• apt-get install ….
• svn co …/git clone …
• puppet …
– 创建生产环境的 virutalenv 作为标准环境(top.prodction)• mkvirutalenv top.production
• mkdir top.production
• 把 top.production.txt 放到 top.production/top.production.txt
– 当前生产环境的第三方库(top.production.txt)• workon top.production
• pip install -r top.production/top.production.txt
– 把上面的内容作为标准的初始化环境发布给开发人员和测试人员
要实现
一个功能搜寻
开源库
验证
是否可用选定
引入代码库开发 测试 上线
0
![Page 18: 01.python.开发最佳实践](https://reader033.vdocuments.pub/reader033/viewer/2022051112/55a0e6901a28ab10248b473b/html5/thumbnails/18.jpg)
• 沙盒环境中做实验(研发工程师)– 从标准 virutalenv 创建沙盒环境用于实验
• cpvirtualenv top.production top.sandbox
– 在沙盒环境中做实验• workon top.sandbox
• pip install …
– 删除并重建复沙盒(每次实验都不会受上一次实验影响)• rmvirutalenv top.sandbox
• cpvirtualenv top.production top.sandbox
– 确定选择后,输出第三方库(清单和文件)• pip freeze > top.sandbox.txt
• mkdir top.sandbox.packages
• pip install -r top.sandbox.txt -d top.sandbox.packages
– 把上面的内容作为标准的初始化环境发布给开发人员和测试人员• 把 top.sandbox.txt 重命名为 top.release.txt 并作为代码一部分提交到代码库
• 把目录中 top.sandbox.packages 文件上传到指定服务器
要实现
一个功能搜寻
开源库
验证
是否可用选定
引入代码库开发 测试 上线
0
1
![Page 19: 01.python.开发最佳实践](https://reader033.vdocuments.pub/reader033/viewer/2022051112/55a0e6901a28ab10248b473b/html5/thumbnails/19.jpg)
• 在测试环境中做测试(测试工程师)
– 创建干净的沙盒
• mkvirtualenv top.testing
– 获取要测试的代码
• workon top.testing
• svn co …
• pip install -r top.release.txt
• 测试
要实现
一个功能搜寻
开源库
验证
是否可用选定
引入代码库开发 测试 上线
0
1
2
![Page 20: 01.python.开发最佳实践](https://reader033.vdocuments.pub/reader033/viewer/2022051112/55a0e6901a28ab10248b473b/html5/thumbnails/20.jpg)
• 在生产环境中部署(运维工程师)– 创建干净的沙盒
• mkvirtualenv top.production
– 安装新代码• workon top.production
• svn co …
• pip install -r top.release.txt
– 更新 production 环境• 实际生产环境运行无问题
• 把 top.release.txt 重命名为 top.production.txt 提交
要实现
一个功能搜寻
开源库
验证
是否可用选定
引入代码库开发 测试 上线
0
1
2 3
![Page 21: 01.python.开发最佳实践](https://reader033.vdocuments.pub/reader033/viewer/2022051112/55a0e6901a28ab10248b473b/html5/thumbnails/21.jpg)
谢谢!