Python venv 环境搭建及 VSCode 环境配置
1 Python venv 环境搭建
1.1 venv 安装
1
2
3
4
5
pip install virtualenv
# 设置永久国内 pip 源
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/
pip config set install.trusted-host pypi.tuna.tsinghua.edu.cn
1.2 创建 venv 环境
1
2
# python -m venv <目录>
python -m venv .
1.3 激活 venv 环境
Linux/Mac:
1
source ./bin/activate
Windows:
1
2
3
4
.\Scripts\activate
# 退出虚拟环境
.\Scripts\deactivate.bat
2. vscode 环境配置
2.1 settings.json 配置
需要安装插件:
- Python
- Pylance
- Yapf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
"terminal.integrated.env.linux": {
"PYTHONPATH": "${workspaceFolder}/python;${env:PYTHONPATH}"
},
"python.envFile": "${workspaceFolder}/.env",
"python.analysis.extraPaths": [
"${workspaceFolder}/python"
],
"[python]": {
"diffEditor.ignoreTrimWhitespace": false,
"editor.defaultFormatter": "eeyore.yapf",
"editor.formatOnSaveMode": "file",
"editor.formatOnSave": true,
"editor.indentSize": 2,
"editor.wordBasedSuggestions": "off",
"files.trimTrailingWhitespace": true,
},
"python.languageServer": "Pylance",
"yapf.args": ["--style", "{based_on_style: pep8, indent_width: 2}"],
2.1 extensions.json 配置
1
2
3
4
5
6
7
8
9
10
11
12
13
{
"recommendations": [
"eeyore.yapf",
"dangmai.workspace-default-settings",
"ms-python.flake8",
"ms-python.isort",
"ms-python.python"
],
"unwantedRecommendations": [
"ms-python.black-formatter",
"ms-python.pylint"
]
}
附件见 python_prj/.vscode.zip
。
本文由作者按照 CC BY 4.0 进行授权