VSCode:无法识别术语“python”……但 py 有效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52332554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:04:33  来源:igfitidea点击:

VSCode: The term 'python' is not recognized...but py works

pythonbuildvisual-studio-codeautomationvscode-code-runner

提问by Bizhan

I just installed python on VS Code and I can't run any python code using pythoncommand.

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

python command:

蟒蛇命令:

It seems to use the pythoncommand by default and it does not recognize it.

它似乎python默认使用该命令并且无法识别它。

When I right click and choose Run Codeit complains:

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

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

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

Same goes for manually running python main.py.

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

When I open an elevated PowerShell and run python, it complains:

当我打开一个提升的 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 command:

py 命令:

It doesn't try to use pycommand but it recognizes it. And when I manually call py main.py, it works.

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

When I manually do >py main.pyit writes my Goodbye, World!

当我手动做>py main.py它写我的Goodbye, World!

Question:

题:

How can I make it compile/run in VS Code simply by using the CodeRunner's right-click feature (Run Code)?

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



I already have both Python folder and its Scripts folder in PATH.

我已经在 PATH 中有 Python 文件夹和它的 Scripts 文件夹。

I'm using VS Code 1.27.2and I have installed python 3.7.0on my machine and have checked its installer checkbox for adding the environment variables automatically. (PATH is ok)

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

I also installed : ms-python.pythonand tht13.pythonand formulahendry.code-runnerextensions on the VS Code.

我还装:ms-python.pythontht13.pythonformulahendry.code-runner对VS代码扩展。



This is my main.pycode:

这是我的main.py代码:

print("Goodbye, World!")

采纳答案by Bizhan

It turned out that I just had to restart my computerafter I installed ms-python.pythonand tht13.pythonand formulahendry.code-runnerextensions on the VS Code and added python's Scriptsfolder in PATH.

原来,我不得不重新启动我的电脑安装后,我ms-python.pythontht13.pythonformulahendry.code-runner扩展的VS代码,增加了Python的Scripts文件夹中PATH

Now both pyand pythoncommands work from anywhere like cmdor Run Codein the right click menu.

现在,pypython命令都可以在任何地方使用,例如右键菜单中cmdRun Code右键菜单中。

回答by Hung Vu

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

在安装 Python 扩展并更改 PATH 以包含 Python 及其脚本文件夹后重新启动您的 PC 会有所帮助。为我工作

回答by Brett Cannon

The Windows installer for Python does not put pythonon your path by default (there's a checkbox during installation to add it). Make sure that you selected an interpreter that's installed by running Select Interpreterand choosing the interpreter you want (the extension will find them through the registry).

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

回答by Daniel

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 pathsettings by running the following from your vs code PowerShell terminal:

如果您已经设置了路径变量,请在命令提示符中测试相同的命令,看看它是否有效。如果是这样,只需path通过从您的 vs 代码 PowerShell 终端运行以下命令来更新 PowerShell 的设置:

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

This trick can save you a lot of restarts.

这个技巧可以为您节省很多重新启动的时间。

回答by TheShauryaSingh

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

您需要首先确认是否安装了 python,为此只需在终端/cmd 上运行 python/python3。

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

如果它在那里运行并且没有在 VS Code 中运行,则重新启动系统以反映更改。

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

如果它也没有在终端/cmd 中运行,那么首先检查 python 的目录是否放置在环境变量中。