问题

VSCode无法正常运行Conda已经配置好的虚拟环境,使用Code Runner快捷键运行的时候报错一直显示找不到某某包

ps:

因为想单独使用minicon管理Python包环境,所以就把之前安装的所有Python都卸载了,但是Python无法识别到这些,所以每次运行的时候都会显示找不到包,因为无法正确识别到自己已经配置好的minicon的Python虚拟环境

解决方法:

一、打开设置(JSON)

  1. Ctrl + ,(设置)
  2. 搜索:code-runner.executorMap
  3. 在 settings.json 中编辑

二、修改 Python 命令

把原来的:

"python": "python -u"

改成下面任意一种(推荐第 1 种):

1. 推荐(和 “运行 Python 文件” 一致)

json

"code-runner.executorMap": {
    "python": "$pythonPath $fullFileName",
    // 其他语言...
}
  • $pythonPath:自动用你当前选的 Python 解释器(虚拟环境 / 全局)
  • $fullFileName:完整路径文件名
  • 效果:和右键 “在终端中运行 Python 文件” 完全一样
2. 保留无缓冲(推荐服务器 / 日志场景)
"python": "$pythonPath -u $fullFileName"
3. 固定 python3(Linux/mac)
"python": "python3 $fullFileName"

三、建议配套设置(更像原生运行)

{
    "code-runner.executorMap": {
        "python": "$pythonPath $fullFileName"
    },
    "code-runner.runInTerminal": true,       // 在集成终端运行(可输入)
    "code-runner.saveFileBeforeRun": true,   // 运行前自动保存
    "code-runner.clearPreviousOutput": true  // 清空旧输出
}

四、保存后

  • 重启 VSCode 或重载窗口:Ctrl+Shift+P → Reload Window
  • 再按 Ctrl+Alt+N 就是 “运行 Python 文件” 模式了。

更多推荐