首页 > 解决方案 > VS Code 未运行 Python 程序,并且在终端上显示 Powershell 广告,并且终端未显示路径

问题描述

问题

一旦我单击“在终端中运行 Python 文件”,它什么也不做,没有错误,什么都没有闲置,但是如果我使用 python IDLE 运行相同的文件,它就可以工作,我重新安装了最新版本的 Python 和VS Code,但问题仍然没有解决。此外,终端会显示 Powershell 广告,并且不会像通常那样显示路径。

编辑:Powershell 显示广告并且无法正常工作

电源外壳

代码在 CMD 中正常运行

命令

我使用的 VS Code 扩展是:

  1. 括号对着色器
  2. 代码运行器
  3. Jupyter(我知道它做了什么,但 VS Code 提示我为 Python 安装它)
  4. 直播服务器
  5. 材质图标主题
  6. Pylance(我知道它做了什么,但 VS Code 提示我为 Python 安装它)
  7. Python

这是 settings.json 的内容

{
    "editor.fontSize": 18,
    "[html]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    },
    "window.zoomLevel": -1,
    "workbench.colorTheme": "Default Dark+",
    "workbench.editorAssociations": [],
    "python.languageServer": "Pylance",
    "editor.mouseWheelZoom": true,
    "code-runner.clearPreviousOutput": true,
    "code-runner.saveAllFilesBeforeRun": true,
    "code-runner.saveFileBeforeRun": true
}

由于这个问题,我忽略了 VS Code 并安装了 PyCharm,我没有遇到任何问题。

标签: pythonvisual-studio-code

解决方案


根据你提供的信息,问题是系统自带的powershell终端无法使用。因为 VS Code 是一个编辑器,它的内部 powershell 终端集成了系统的 powershell 终端。不过VS Code不仅支持这个终端,还支持cmd终端、bash等。

解决方案:由于可以在 VS Code 之外的 cmd 终端中运行 python 代码,请使用

 "terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\cmd.exe",

代替

"terminal.integrated.shell.windows": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",

在 VS Code 的“settings.json”中将终端切换到 cmd。

或者你可以使用下面的方法,也可以将终端切换到cmd:

在此处输入图像描述

参考: VS Code 中的集成终端


推荐阅读