VSCode搭建python环境记录

昨晚自己在使用VSCode搭建python环境的时候,因为某个路径配置错误,导致整个环境都不能用了。
其实,我只是想,使用虚拟环境。
按照网上的教程,我又重新捣鼓了几遍。在安装code runner插件的时候,始终无法使用虚拟环境运行脚本。
在网上搜索查找方法,终于找到了解决方法。需要在.vcode下的tasks.json里配置虚拟路径位置,配置如下:

{
   // See https://go.microsoft.com/fwlink/?LinkId=733558
   // for the documentation about the tasks.json format
   "version": "2.0.0",
   "tasks": [
       {
           "label": "Python",
           "type": "shell",
           "command": "D:\\software\\other\\python\\virtualenv\\venv\\Scripts\\python.exe",
           "args": [
               "${file}"
           ],
           "presentation": {
               "reveal": "always",
               "panel": "shared"
           },
           "group": {
               "kind": "build",
               "isDefault": true
           }
       }
   ]
}

另外settins.json也需要配置,配置如下:

 {
    "python.pythonPath": "D:\\software\\other\\python\\virtualenv\\venv\\Scripts\\python.exe",
    "code-runner.executorMap": {
        "python": "D:\\software\\other\\python\\virtualenv\\venv\\Scripts\\python.exe"
    },
    "python.venvPath": "D:\\software\\other\\python\\virtualenv",
    "python.venvFolders": [
        "venv"
    ]
}

总之,都配置成虚拟环境的路径就好。launch.json下面的配置可以不管。
.code-workspace文件的配置也可以配置成虚拟路径,配置如下:

 {
    "folders": [
        {
            "path": "."
        }
    ],
    "settings": {
        "python.pythonPath": "D:\\software\\other\\python\\virtualenv\\venv\\Scripts\\python.exe"
    }
}

然后我写了一个来测试,通过run code插件来执行脚本,执行结果如下:


run code

结果为:


指向虚拟路径执行

执行的时候是去执行虚拟环境的python的。暂时算解决,感谢网上各位提供的方法。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容