昨晚自己在使用VSCode搭建python環(huán)境的時候,因?yàn)槟硞€路徑配置錯誤,導(dǎo)致整個環(huán)境都不能用了。
其實(shí),我只是想,使用虛擬環(huán)境。
按照網(wǎng)上的教程,我又重新?lián)v鼓了幾遍。在安裝code runner插件的時候,始終無法使用虛擬環(huán)境運(yùn)行腳本。
在網(wǎng)上搜索查找方法,終于找到了解決方法。需要在.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"
]
}
總之,都配置成虛擬環(huán)境的路徑就好。launch.json下面的配置可以不管。
.code-workspace文件的配置也可以配置成虛擬路徑,配置如下:
{
"folders": [
{
"path": "."
}
],
"settings": {
"python.pythonPath": "D:\\software\\other\\python\\virtualenv\\venv\\Scripts\\python.exe"
}
}
然后我寫了一個來測試,通過run code插件來執(zhí)行腳本,執(zhí)行結(jié)果如下:

run code
結(jié)果為:

指向虛擬路徑執(zhí)行
執(zhí)行的時候是去執(zhí)行虛擬環(huán)境的python的。暫時算解決,感謝網(wǎng)上各位提供的方法。