如何在Visual Studio Code中配置Python编程环境

编程语言:python
集成开发环境(IDE):Visual Studio Code

目录

  • 识别Python语言
  • 解释Python语言
  • 下载Python解释器
  • 安装
  • 检查是否安装成功
  • *备忘:配置环境变量*
  • 创建程序文件(.py)
  • 准备运行环境
  • 创建虚拟环境
  • 在虚拟环境下工作
  • 使用pip安装程序包
  • 识别Python语言

    在VScode中安装Python插件(Extensions)
    Python插件

    解释Python语言

    python是一种脚本语言,不需要编译,由解释器负责解释成计算机能处理的机器语言

    下载Python解释器

    Python官方下载网站 – Download the latest version for Windows

    安装

    Python安装界面
    ①勾选Add python.exe to PATH,添加环境变量
    ②选择自定义安装
    自定义安装界面
    ①默认勾选,Python文档,必须安装
    ②默认勾选,pip.exe,建议安装,用于从PYPI(Python Package Index)上下载和安装Python第三方包(packages)

    In Python, packages are how you obtain any number of useful code libraries, typically from PyPI.

    ③默认开发环境,Python自带的集成开发环境IDLE,建议安装

    检查是否安装成功

    打开命令提示符(cmd),输入python,按Enter
    如果安装成功并且已经配置环境变量,则显示python版本
    安装成功

    备忘:配置环境变量

    #mermaid-svg-ccqLsEx3Y0kWqpKK {font-family:”trebuchet ms”,verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-ccqLsEx3Y0kWqpKK .error-icon{fill:#552222;}#mermaid-svg-ccqLsEx3Y0kWqpKK .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-ccqLsEx3Y0kWqpKK .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-ccqLsEx3Y0kWqpKK .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-ccqLsEx3Y0kWqpKK .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-ccqLsEx3Y0kWqpKK .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-ccqLsEx3Y0kWqpKK .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-ccqLsEx3Y0kWqpKK .marker{fill:#333333;stroke:#333333;}#mermaid-svg-ccqLsEx3Y0kWqpKK .marker.cross{stroke:#333333;}#mermaid-svg-ccqLsEx3Y0kWqpKK svg{font-family:”trebuchet ms”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-ccqLsEx3Y0kWqpKK .label{font-family:”trebuchet ms”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-ccqLsEx3Y0kWqpKK .cluster-label text{fill:#333;}#mermaid-svg-ccqLsEx3Y0kWqpKK .cluster-label span{color:#333;}#mermaid-svg-ccqLsEx3Y0kWqpKK .label text,#mermaid-svg-ccqLsEx3Y0kWqpKK span{fill:#333;color:#333;}#mermaid-svg-ccqLsEx3Y0kWqpKK .node rect,#mermaid-svg-ccqLsEx3Y0kWqpKK .node circle,#mermaid-svg-ccqLsEx3Y0kWqpKK .node ellipse,#mermaid-svg-ccqLsEx3Y0kWqpKK .node polygon,#mermaid-svg-ccqLsEx3Y0kWqpKK .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ccqLsEx3Y0kWqpKK .node .label{text-align:center;}#mermaid-svg-ccqLsEx3Y0kWqpKK .node.clickable{cursor:pointer;}#mermaid-svg-ccqLsEx3Y0kWqpKK .arrowheadPath{fill:#333333;}#mermaid-svg-ccqLsEx3Y0kWqpKK .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-ccqLsEx3Y0kWqpKK .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-ccqLsEx3Y0kWqpKK .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-ccqLsEx3Y0kWqpKK .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-ccqLsEx3Y0kWqpKK .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-ccqLsEx3Y0kWqpKK .cluster text{fill:#333;}#mermaid-svg-ccqLsEx3Y0kWqpKK .cluster span{color:#333;}#mermaid-svg-ccqLsEx3Y0kWqpKK div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:”trebuchet ms”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-ccqLsEx3Y0kWqpKK :root{–mermaid-font-family:”trebuchet ms”,verdana,arial,sans-serif;}

    设置

    高级系统设置

    高级

    环境变量

    环境变量
    如果已有Path变量,点击编辑(如果没有,新建一个Path变量)
    编辑环境变量
    新建2个路径:
    ①Python的安装路径:配置Python.exe的环境变量
    ②Python的安装路径\Scripts:配置pip.exe的环境变量

    创建程序文件(.py)

    新建一个文件夹作为Python工作区(WorkSpace),用于存放Python文件
    在VSCode > File > Open Folder中打开该文件夹
    在VSCode > file > New File中新建一个Python文件(.py),存在该文件夹中

    准备运行环境

    用Python语言编写的程序必须在Python环境中运行,环境中包含一个解释器和一系列程序包。
    默认情况下,安装的任何Python解释器都在自己的全局环境中运行,在全局环境中安装或卸载的任何程序包都会影响其中运行的所有程序,在全局环境中安装太多程序包会导致所有程序运行缓慢。
    为了解决该问题,可以为每一个工作区创建一个虚拟环境,虚拟环境中会包含解释器的一个副本,而在该虚拟环境中安装或卸载程序包都只影响当前工程中的程序。

    #mermaid-svg-KLaCaa5dS6PWwdVy {font-family:”trebuchet ms”,verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-KLaCaa5dS6PWwdVy .error-icon{fill:#552222;}#mermaid-svg-KLaCaa5dS6PWwdVy .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-KLaCaa5dS6PWwdVy .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-KLaCaa5dS6PWwdVy .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-KLaCaa5dS6PWwdVy .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-KLaCaa5dS6PWwdVy .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-KLaCaa5dS6PWwdVy .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-KLaCaa5dS6PWwdVy .marker{fill:#333333;stroke:#333333;}#mermaid-svg-KLaCaa5dS6PWwdVy .marker.cross{stroke:#333333;}#mermaid-svg-KLaCaa5dS6PWwdVy svg{font-family:”trebuchet ms”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-KLaCaa5dS6PWwdVy .label{font-family:”trebuchet ms”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-KLaCaa5dS6PWwdVy .cluster-label text{fill:#333;}#mermaid-svg-KLaCaa5dS6PWwdVy .cluster-label span{color:#333;}#mermaid-svg-KLaCaa5dS6PWwdVy .label text,#mermaid-svg-KLaCaa5dS6PWwdVy span{fill:#333;color:#333;}#mermaid-svg-KLaCaa5dS6PWwdVy .node rect,#mermaid-svg-KLaCaa5dS6PWwdVy .node circle,#mermaid-svg-KLaCaa5dS6PWwdVy .node ellipse,#mermaid-svg-KLaCaa5dS6PWwdVy .node polygon,#mermaid-svg-KLaCaa5dS6PWwdVy .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-KLaCaa5dS6PWwdVy .node .label{text-align:center;}#mermaid-svg-KLaCaa5dS6PWwdVy .node.clickable{cursor:pointer;}#mermaid-svg-KLaCaa5dS6PWwdVy .arrowheadPath{fill:#333333;}#mermaid-svg-KLaCaa5dS6PWwdVy .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-KLaCaa5dS6PWwdVy .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-KLaCaa5dS6PWwdVy .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-KLaCaa5dS6PWwdVy .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-KLaCaa5dS6PWwdVy .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-KLaCaa5dS6PWwdVy .cluster text{fill:#333;}#mermaid-svg-KLaCaa5dS6PWwdVy .cluster span{color:#333;}#mermaid-svg-KLaCaa5dS6PWwdVy div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:”trebuchet ms”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-KLaCaa5dS6PWwdVy :root{–mermaid-font-family:”trebuchet ms”,verdana,arial,sans-serif;}

    全局环境

    虚拟环境1

    虚拟环境2

    虚拟环境3

    创建虚拟环境

    Ctrl + Shift + P打开命令面板(Command Palette),输入Python: Create Environment
    选择环境类型
    选择用Venv创建虚拟环境
    选择计时器
    选择用于创建虚拟环境的解释器

    以上操作等价于在Terminal中输入如下代码:

    python -m venv .venv
    

    虚拟环境创建以后该文件夹中会增加一个名称为.venv的文件夹

    在虚拟环境下工作

    Ctrl + Shift + P打开命令面板,输入Python: Select Interpreter
    选择解释器
    选择虚拟环境中的解释器,则虚拟环境被激活(再次用Vscode打开该文件夹中的Python文件时,会默认在上次选择的环境中运行)

    选择的解释器会显示在界面右下角的状态栏中:
    状态栏

    在Terminal中激活虚拟环境的命令如下:

    .venv\scripts\activate
    

    在Terminal中关闭虚拟环境的命令如下:

    deactivate
    

    使用pip安装程序包

    创建虚拟环境后,.venv > Script 文件夹下会有pip.exe文件,推荐在虚拟环境中安装所需要的程序包

    在Terminal中输入以下代码,将pip更新到最新版本

    python -m pip install --upgrade pip
    

    显示Requirement already satisfied表示pip已经是最新版本,如下图所示:
    最新版

    在Terminal中安装程序包的命令如下:

    python -m pip install xxx
    
    物联沃分享整理
    物联沃-IOTWORD物联网 » 如何在Visual Studio Code中配置Python编程环境

    发表评论