Python venv 环境搭建及 VSCode 环境配置

1. Python venv 环境搭建

1.1. venv安装

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环境

# python -m venv <目录>
python -m venv .venv

1.3. 激活venv环境

Linux/Mac:

source ./.venv/bin/activate

Windows:

.\.venv\Scripts\activate

# 退出虚拟环境
.\.venv\Scripts\deactivate.bat

1.4. 安装依赖

# pip install -r requirements.txt
# pip install pygame

2. vscode 环境配置

2.1. settings.json配置

需要安装插件:

  • Python
  • Pylance
  • Yapf
"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.2. extensions.json配置

{
  "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




    Enjoy Reading This Article?

    Here are some more articles you might like to read next:

  • 在Ubuntu上部署OpenMAIC
  • AI工具大全
  • Fast DDS入门(On-Going)
  • NVIDIA GPU 架构:SP、SM 与 LSU 工作原理详解
  • al-folio 模板定制修改总结