18

我刚刚在 VS Code 上安装了 python,我无法使用python命令运行任何 python 代码。

蟒蛇命令:

运行代码似乎python默认运行命令并且它无法识别它。

当我右键单击并选择Run Code它时抱怨:

'python' is not recognized as an internal or external command, operable program or batch file

手动运行也是如此python main.py

当我打开提升的 PowerShell 并运行python时,它会抱怨:

python : The term 'python' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ python
+ ~~~~~~
    + CategoryInfo          : ObjectNotFound: (python:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

py 命令:

它不会尝试使用py命令,但它会识别它。当我手动调用时py main.py,它可以工作

当我手动执行>py main.py时,它会写入我的Goodbye, World!

问题:

如何通过使用 CodeRunner 的右键单击功能 ( Run Code) 使其在 VS Code 中编译/运行?


我已经在 PATH 中有 Python 文件夹及其 Scripts 文件夹。

我正在使用VS Code 1.27.2并且我已经python 3.7.0在我的机器上安装并检查了它的安装程序复选框以自动添加环境变量。(路径没问题)

我还在 VS Code 上安装了 : ms-python.pythonand tht13.pythonandformulahendry.code-runner扩展。


这是我的main.py代码:

print("Goodbye, World!")
4

7 回答 7

13

It turned out that I just had to restart my computer after I installed ms-python.python and tht13.python and formulahendry.code-runner extensions on the VS Code and added python's Scripts folder in PATH.

Now both py and python commands work from anywhere like cmd or Run Code in the right click menu.

于 2018-09-19T14:36:12.893 回答
4

Restarting your PC after installing the Python Extension and changing the PATH to include Python and it's scripts folder will help. Worked for me

于 2019-05-02T04:47:18.780 回答
2

默认情况下,Python 的 Windows 安装程序不会放在python您的路径上(安装过程中有一个复选框可以添加它)。确保您选择了通过运行Select Interpreter并选择所需的解释器来安装的解释器(扩展程序将通过注册表找到它们)。

于 2018-09-18T22:01:05.890 回答
2

I also had this problem after a fresh Windows reinstallation, vscode didnt recognize the commands like python or pip freeze in the PS terminal.

After reinstalling python and vscode, I read the tutorial for python for vscode: https://code.visualstudio.com/docs/python/python-tutorial. Creating a new venv worked for me py -3 -m venv .venv, then navigate to the venv: .venv\scripts\activate. In the new venv all the python commands worked as normal.

于 2020-06-17T08:29:01.210 回答
1

If you have already set the path variable, test the same command in a command prompt and see if it works. If it does, just update PowerShell's path settings by running the following from your vs code PowerShell terminal:

$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" +
            [System.Environment]::GetEnvironmentVariable("Path","User")`

This trick can save you a lot of restarts.

于 2020-05-18T18:52:47.483 回答
0

you need to first confirm if python is installed, for that just run python/python3 on terminal/cmd.

If it runs there and it isn't running in VS Code then restart your system in order to get changes reflected.

And if it doesn't run in terminal/cmd as well then first check if python's directories are placed in environment variables.

于 2020-04-07T11:18:24.887 回答
-1

Add Python path (ex C:\Users\johndoe\AppData\Local\Programs\Python\Python39) to the %PATH% env variable

于 2021-10-03T13:57:31.583 回答