虚拟环境
安装
$ pip install virtualenv
$ pip install virtualenvwrapper-win # windows下的管理工具
$ pip install virtualenvwrapper
$ pip install pipenv
查看已安装的虚拟环境
$ workon
Windows修改安装的位置
新增环境变量
变量名:
WORKON_HOME
变量值:虚拟环境存放路径
Linux修改安装的位置
# pip后建立软连接
$ ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualenv
# Linux配置虚拟环境路径
$ vim ~/.bashrc
# mac配置
$ vim ~/.bash_profile
# 新增
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/python3/bin/virtualenvwrapper.sh
# 执行命令,让配置生效
# Linux
source ~/.bashrc
# mac
source ~/.bash_profile
创建虚拟环境
-p
指定解释器
$ mkvirtualenv -p python3 test_py3
进入虚拟环境
$ workon test_py3
退出虚拟环境
$ deactivate
删除虚拟环境
$ rmvirtualenv test_py3
导出虚拟环境中所有的模块和包
$ pip frezz >requirements.txt
安装requirements.txt中的包
$ pip install -r requirements.txt
virtualenv存在的问题
virtualenv卸载一个包之后,相关依赖不会被同步卸载
pipenv
与项目绑定
创建虚拟环境
$ pipenv install
进入虚拟环境
$ pipenv shell
退出虚拟环境
exit
安装包
在哪个路径下操作,就安装到哪个环境
$ pipenv install requests
查看依赖
$ pipenv graph
卸载包
当前环境的依赖不会被卸载
移植Pipfile
和Pipfile.lock
后重新生成不会携带
$ pipenv uninstall requests
开发环境安装
$ pipenv install --dev requests
导出包
$ pipenv lock -r >requirements.txt
删除虚拟环境
Pipfile
和Pipfile.lock
不会被删除
$ 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个空格
# 左对齐
foo = long_function_name(var_one, var_two,
var_three, var_four)
# 换行缩进
foo = long_function_name(
var_one, var_two,
var_three, var_four)
# 用更多的缩进来与其他行区分
foo = long_function_name(
var_one, var_two, var_three,
var_four)
行的最大长度
最大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