VSCode搭建python环境记录

2019-04-25  本文已影响0人  邪恶的正派

昨晚自己在使用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的。暂时算解决,感谢网上各位提供的方法。
上一篇下一篇

猜你喜欢

热点阅读