之前用的是PyCharm,但是这段时间注册码过期了,又找不到新的注册码,干脆暂时弃坑,转而投入VScode的大家庭。

下载安装VScode,python以及在VScode中安装python的插件,codeRunner插件这里就不赘述了,百度一下都有。

这里我在E盘下新建一个文件夹:mypython,在mypython下新建一个py空文件,main.py,并建立一个空文件venv,

打开cmd,进入安装python 的路径下,我这里装的3.7版本,

cd C:\Users\你的用户名\AppData\Local\Programs\Python\Python37\Scripts

安装virtulenv

pip install virtualenv

创建虚拟环境

C:\Users\你的用户名\AppData\Local\Programs\Python\Python37\Scripts\virtualenv  E:\mypython\venv

其实这后面还可以加一些命令,感兴趣的可以 试一下命令,virtualenv  -h,

接下来 VScode中打开设置(ctrl+,),输入python:env

conda虚拟环境 vscode使用python vscode创建虚拟环境python_python

如上图所示,选择workspace,python:venv path 就填写存放虚拟环境的文件夹,最后一步填写Add Item,就填写虚拟环境venv

然后此时我们点VScode左下角,选择我们的虚拟环境

conda虚拟环境 vscode使用python vscode创建虚拟环境python_虚拟环境_02

conda虚拟环境 vscode使用python vscode创建虚拟环境python_虚拟环境_03

如上图,选择第二个也就是我们刚搭建的虚拟环境。

后面还需要配置一下文件,win7和win10 是有区别的,

在.vscode文件夹下,launch.json

{    
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        
        {"name":"Python: 当前文件","type":"python","request":"launch","program":"${file}","console":"integratedTerminal"},
        {
            "name": "Python: 当前文件",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "externalTerminal",//integratedTerminal" 选择内外置终端
        }
    ]
}

win10和win7下的settings.json文件有所区别

win10版

{
   "python.venvPath": "E:\\mypython",
    "python.venvFolders": [
        "venv"
    ],
    "python.pythonPath": "E:\\mypython\\venv\\Scripts\\python.exe",
    //"terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe",
    
        "files.defaultLanguage": "python", // ctrl+N新建文件后默认的语言
        "editor.formatOnType": false, // 输入分号(C/C++的语句结束标识)后自动格式化当前这一行的代码
        "editor.suggest.snippetsPreventQuickSuggestions": false, // clangd的snippets有很多的跳转点,不用这个就必须手动触发Intellisense了
        "editor.acceptSuggestionOnEnter": "off", // 我个人的习惯,按回车时一定是真正的换行,只有tab才会接受Intellisense
        // "editor.snippetSuggestions": "top", // (可选)snippets显示在补全列表顶端,默认是inline
        "code-runner.runInTerminal": true, // 设置成false会在“输出”中输出,无法输入
        "code-runner.executorMap": {
            // "c": "cd $dir && gcc '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc -std=c11 -fexec-charset=GBK && &'$dir$fileNameWithoutExt'",
            //"cpp": "cd $dir && g++ '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc -std=c++17 -fexec-charset=GBK && &'$dir$fileNameWithoutExt'"
           "python":"python -u",
        }, // 右键run code时运行的命令;未注释的仅适用于PowerShell(Win10默认),文件名中有空格也可以编译运行;注释掉的适用于cmd(win7默认),PS和bash也能用,但文件名中有空格时无法运行
        "code-runner.saveFileBeforeRun": true, // run code前保存
        "code-runner.preserveFocus": true, // 若为false,run code后光标会聚焦到终端上。如果需要频繁输入数据可设为false
        "code-runner.clearPreviousOutput": false, // 每次run code前清空属于code runner的终端消息,默认false
        "code-runner.ignoreSelection": true, // 默认为false,效果是鼠标选中一块代码后可以单独执行,但C是编译型语言,不适合这样用
        // "terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe",
        //"terminal.integrated.shell.windows": "powershell.exe",
}

win7版

{
 "python.venvPath": "E:\\mypython",
    "python.venvFolders": [
        "venv"
    ],
    "python.pythonPath": "E:\\mypython\\venv\\Scripts\\python.exe",
    //"terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe",
    
        "files.defaultLanguage": "python", // ctrl+N新建文件后默认的语言
        "editor.formatOnType": false, // 输入分号(C/C++的语句结束标识)后自动格式化当前这一行的代码
        "editor.suggest.snippetsPreventQuickSuggestions": false, // clangd的snippets有很多的跳转点,不用这个就必须手动触发Intellisense了
        "editor.acceptSuggestionOnEnter": "off", // 我个人的习惯,按回车时一定是真正的换行,只有tab才会接受Intellisense
        // "editor.snippetSuggestions": "top", // (可选)snippets显示在补全列表顶端,默认是inline
        "code-runner.runInTerminal": true, // 设置成false会在“输出”中输出,无法输入
        "code-runner.executorMap": {
            // "c": "cd $dir && gcc '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc -std=c11 -fexec-charset=GBK && &'$dir$fileNameWithoutExt'",
            //"cpp": "cd $dir && g++ '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc -std=c++17 -fexec-charset=GBK && &'$dir$fileNameWithoutExt'"
           "python":"python -u",
        }, // 右键run code时运行的命令;未注释的仅适用于PowerShell(Win10默认),文件名中有空格也可以编译运行;注释掉的适用于cmd(win7默认),PS和bash也能用,但文件名中有空格时无法运行
        "code-runner.saveFileBeforeRun": true, // run code前保存
        "code-runner.preserveFocus": true, // 若为false,run code后光标会聚焦到终端上。如果需要频繁输入数据可设为false
        "code-runner.clearPreviousOutput": false, // 每次run code前清空属于code runner的终端消息,默认false
        "code-runner.ignoreSelection": true, // 默认为false,效果是鼠标选中一块代码后可以单独执行,但C是编译型语言,不适合这样用
       //  "terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe",
        //"terminal.integrated.shell.windows": "powershell.exe", 
}

最后,win10版本可能会遇到activate.ps1文件无法执行,可以参考前面的连接修改,有可能需要以管理员身份运行命令

set-executionpolicy remotesigned

当然你要想改回来,命令改一下就行

set-executionpolicy Restricted

参考链接:

https://www.zhihu.com/question/30315894/answer/154979413