测试开发进阶(一)
虚拟环境
安装
1 | pip install virtualenv |
查看已安装的虚拟环境
1 | workon |
Windows修改安装的位置
新增环境变量
- 变量名:
WORKON_HOME
- 变量值:虚拟环境存放路径
Linux修改安装的位置
1 | pip后建立软连接 |
创建虚拟环境
-p
指定解释器
1 | mkvirtualenv -p python3 test_py3 |
进入虚拟环境
1 | workon test_py3 |
退出虚拟环境
1 | deactivate |
删除虚拟环境
1 | rmvirtualenv test_py3 |
导出虚拟环境中所有的模块和包
1 | pip frezz >requirements.txt |
安装requirements.txt中的包
1 | pip install -r requirements.txt |
virtualenv存在的问题
virtualenv卸载一个包之后,相关依赖不会被同步卸载
pipenv
与项目绑定
创建虚拟环境
1 | pipenv install |
进入虚拟环境
1 | pipenv shell |
退出虚拟环境
1 | exit |
安装包
在哪个路径下操作,就安装到哪个环境
1 | pipenv install requests |
查看依赖
1 | pipenv graph |
卸载包
当前环境的依赖不会被卸载
移植Pipfile
和Pipfile.lock
后重新生成不会携带
1 | pipenv uninstall requests |
开发环境安装
1 | pipenv install --dev requests |
导出包
1 | pipenv lock -r >requirements.txt |
删除虚拟环境
Pipfile
和Pipfile.lock
不会被删除
1 | pipenv --rm |
编码规范
python之禅
import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than right now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea – let’s do more of those!
缩进
- 每一级缩进使用4个空格
1 | # 左对齐 |
行的最大长度
- 最大79
- 注释最大72
空行
- 函数之间2个空行
- 类的方法之间1个空行
注释
块注释
与代码在同一行
行内注释
文档注释
第一行:对函数/类整体功能说明
参数说明
:param var_one: 参数1 是干嘛的
:type var_one:int
返回值说明
:return:
PEP257:https://github.com/qiuxiang/pep/blob/master/peps/257.md
模块包导入
- 先导入python内置模块和包
- 导入第三方的模块和包
- 导入自定义的模块和包
__all__=[]
使用*
导入时候只会导入all的[]中的的内容
结构化工程
https://pythonguidecn.readthedocs.io/zh/latest/writing/structure.html