2021-03-27

Windows下 vscode 配置Python3开发环境

一 、下载python

url地址 : https://www.python.org/downloads/windows/

二、安装python

由于Windows安装Python很简单,我就不多说了,只说一个很重要的提示: 安装过程中,记得勾选  添加环境变量。

三、 vscode 安装所需插件

1、插件名称: python ;

插件功能: 这个是vscode提供的python 官方插件,提供了python代码的调试,自动补全,代码格式化等功能。

2、插件名称: vscode-icons;

插件功能: 这个也是vscode官方提供的插件,作用是给vscode编辑的文件增加图标。

3、插件名称:Path Intellisense ;

插件功能:这个插件的作用是当代码中读入文件名或者文件路径时,提供文件名或者文件路径的自动补全。

4、插件名称:Guides ;

插件功能: 这个插件的作用是增加 .py 中的指示线,用这个插件能让代码的层次结构更加清晰。

5、插件名称:Bracket Pair Colorizer ;

插件功能: 这个插件的作用是给代码中的括号增加颜色,同一对括号是相同的颜色,尤其是在括号中还包着括号的时候,看起来更加的清晰。

6、插件名称: topper ;

插件功能: 这个插件的作用是在.py文件的开头添加一些说明header。

四 、 配置

1、创建python文件夹

vscode 是基于文件夹的编辑器,我们可以首先建立一个文件夹叫做python,作为我们的Python编程工作空间,只要一次配置好了这个工作空间,以后这个工作空间的配置就会对它之下的所有的.py 文件都起作用。

打开vscode,点击左上角文件 —> 打开文件夹,然后打开刚刚建立的PYTHON 文件夹。新建一个 hello.py 文件。

2、配置 launch.json 文件

点击菜单栏调试 —> 打开配置,就会弹出一个选择框,我们在这里要选择Python,然后就打开了launch.json 文件:

我们看到的launch.json 文件中的内容如上图所示。同时我们还发现,在python工作区PYTHON下面还多了一个文件夹.vscode, 而且launch.json 就在这个文件夹中。

对launch.json 文件的配置如下: 这是我配置的全部内容,可以参考一下:

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python3",
            "type": "python",
            "request": "launch",
            "stopOnEntry": false,
            "pythonPath": "C:/Users/Administrator/AppData/Local/Programs/Python/Python37", //python3的安装路径
            "program": "${file}",
            "cwd": "${workspaceFolder}",
            "env": {},
            "envFile": "${workspaceFolder}/.env",
            "debugOptions": [
                "RedirectOutput"
            ]
        },
        {
            "name": "Python: Attach",
            "type": "python",
            "request": "attach",
            "localRoot": "${workspaceFolder}",
            "remoteRoot": "${workspaceFolder}",
            "port": 3000,
            "secret": "my_secret",
            "host": "localhost"
        },
        {
            "name": "Python: Terminal (integrated)",
            "type": "python",
            "request": "launch",
            "stopOnEntry": false,
            "pythonPath": "C:/Users/Administrator/AppData/Local/Programs/Python/Python37",
            "program": "${file}",
            "cwd": "",
            "console": "integratedTerminal",
            "env": {},
            "envFile": "${workspaceFolder}/.env",
            "debugOptions": []
        },
        {
            "name": "Python: Terminal (external)",
            "type": "python",
            "request": "launch",
            "stopOnEntry": false,
            "pythonPath": "C:/Users/Administrator/AppData/Local/Programs/Python/Python37",
            "program": "${file}",
            "cwd": "",
            "console": "externalTerminal",
            "env": {},
            "envFile": "${workspaceFolder}/.env",
            "debugOptions": []
        },
        {
            "name": "Python: Django",
            "type": "python",
            "request": "launch",
            "stopOnEntry": true,
            "pythonPath": "C:/Users/Administrator/AppData/Local/Programs/Python/Python37",
            "program": "${workspaceFolder}/manage.py",
            "cwd": "${workspaceFolder}",
            "args": [
                "runserver",
                "--noreload",
                "--nothreading"
            ],
            "env": {},
            "envFile": "${workspaceFolder}/.env",
            "debugOptions": [
                "RedirectOutput",
                "Django"
            ]
        },
        {
            "name": "Python: Flask (0.11.x or later)",
            "type": "python",
            "request": "launch",
            "module": "flask",
            "env": {
                "FLASK_APP": "app.py"
            },
            "args": [
                "run",
                "--no-debugger",
                "--no-reload"
            ]
        },
        {
            "name": "Python: Module",
            "type": "python",
            "request": "launch",
            "module": "module.name"
        },
        {
            "name": "Python: Pyramid",
            "type": "python",
            "request": "launch",
            "args": [
                "${workspaceFolder}/development.ini"
            ],
            "debugOptions": [
                "RedirectOutput",
                "Pyramid"
            ]
        },
        {
            "name": "Python: Watson",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/console.py",
            "args": [
                "dev",
                "runserver",
                "--noreload=True"
            ]
        },
        {
            "name": "Python: All debug Options",
            "type": "python",
            "request": "launch",
            "pythonPath": "${config:python.pythonPath}",
            "program": "${file}",
            "module": "module.name",
            "env": {
                "VAR1": "1",
                "VAR2": "2"
            },
            "envFile": "${workspaceFolder}/.env",
            "args": [
                "arg1",
                "arg2"
            ],
            "debugOptions": [
                "RedirectOutput"
            ]
        }
    ]
}

 

3、配置 tasks.json 文件

点击菜单栏任务 —> 配置任务,就会弹出一个选择框,我们在这里要选择使用模板创建tasks.json文件,然后又弹出一个选择框,这里选择Others,就打开了tasks.json 文件:

有的一些vscode软件需要从菜单栏“ 终端”菜单中运行“ 配置任务”,然后从模板条目中选择Others“ 创建tasks.json”文件

 

配置修改如下:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "python3",
            "type": "shell",
            "command": "C:/Users/Administrator/AppData/Local/Programs/Python/Python37",
            "args": [
                "${file}"
            ]
        }
    ]
}

 

4、用户设置

点击菜单栏文件 —> 首选项—> 设置,然后打开用户设置:

{
    "window.zoomLevel": 1,
    "files.autoSave": "afterDelay",
    "explorer.confirmDragAndDrop": false,"editor.renderIndentGuides": false,
    "git.ignoreLegacyWarning": true,
    "workbench.iconTheme": "vscode-icons", //启用vscode图标
    "python.pythonPath": "/usr/bin/python3", // python3路径
    "editor.lineHeight": 26, // 编辑器中的行高
    "editor.fontSize": 18, // 编辑器中的字体
    "editor.wordWrap": "on",
    "editor.formatOnSave": true, //编辑器自动保存
    "python.linting.flake8Enabled": true,
    "python.linting.enabled": false,
    //启用flake8,首先需要pip3 install falke8
    "python.formatting.provider": "yapf", ///启用yapf,首先需要pip3 install yapf
    "path-intellisense.autoSlashAfterDirectory": true,
    "path-intellisense.extensionOnImport": true,
    "workbench.colorTheme": "Monokai", // 配色方案
    "python.linting.pylintArgs": [
        "--load-plugins",
        "pylint_django",
        "--disable-msg=C0111"
    ], // 忽略的警告信息
    // 下面是topper的插入header配置
    "topper.customTemplateParameters": [
        {
            "personalProfile": {
                "author": "你的名字",
                "website": "bulbasaur.github.bitbucket.yababbdadado.com",
                "copyright": "None \n None",
                "license": "None",
                "email": "你的邮箱"
            }
        },
        {
            "officeProfile": {
                "author": "John Doe",
                "department": "Product Development",
                "email": "john.doe@doejohn.com"
            }
        }
    ],
    "topper.headerTemplates": [
        {
            "defaultCStyled": {
                "headerBegin": "/**",
                "headerPrefix": "*",
                "headerEnd": "*/",
                "template": [
                    "${headerBegin}",
                    "${headerPrefix} ${fileName}",
                    "${headerPrefix} @author ${author}",
                    "${headerPrefix} @description ${description}",
                    "${headerPrefix} @created ${createdDate}",
                    "${headerPrefix} @copyright ${copyright}",
                    "${headerPrefix} @last-modified ${lastModifiedDate}",
                    "${headerEnd}"
                ]
            }
        },
        {
            "python": {
                "headerBegin": "# -*- coding: utf-8 -*-",
                "headerPrefix": "#",
                "headerEnd": "#",
                "template": [
                    "${headerBegin}",
                    "${headerPrefix} ${fileName}",
                    "${headerPrefix} @author ${author}",
                    "${headerPrefix} @description ${description}",
                    "${headerPrefix} @created ${createdDate}",
                    "${headerPrefix} @last-modified ${lastModifiedDate}",
                    "${headerEnd}"
                ]
            }
        }
    ],
    "editor.fontFamily": "monospace",
    "terminal.integrated.fontFamily": "monospace",
    "editor.fontWeight": "500",
}

 

五 、 其他

 

1、需要更新一下pip,cmd 下 执行命令:

python -m pip install --upgrade pip

 

2、安装 flake 8,cmd 下 执行命令:

pip install flake8

 

3、查看python 安装路径:

>>> import sys
>>> sys.path
['', 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\pyth
on37.zip', 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37
\\DLLs', 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\
lib', 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37', 'C
:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-p
ackages']

最终结果: C:\Users\Administrator\AppData\Local\Programs\Python\Python37

 

配置到此结束,可以愉快玩耍了!

上一篇:VSCode 如何运行调试全局安装的 Electron


下一篇:配置VScode调试配置文件