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

Question:

I just installed python on VS Code and I can’t run any python code using python command.

python command:

Running the code seems to run python command by default and it does not recognize it.

When I right click and choose Run Code it complains:

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

Same goes for manually running python main.py.

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

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:

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

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

Question:

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


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

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

I also installed : ms-python.python and tht13.python and formulahendry.code-runner extensions on the VS Code.


This is my main.py code:

print("Goodbye, World!")
Asked By: Bizhan

||

Answers:

The Windows installer for Python does not put python on 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 Interpreter and choosing the interpreter you want (the extension will find them through the registry).

Answered By: Brett Cannon

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.

Answered By: Bizhan

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

Answered By: Hung Vu

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.

Answered By: TheShauryaSingh

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.

Answered By: Daniel

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: .venvscriptsactivate. In the new venv all the python commands worked as normal.

Answered By: bernardhilbink87

Add Python path (ex C:UsersjohndoeAppDataLocalProgramsPythonPython39) to the %PATH% env variable

Answered By: Nando

I added PATH and did everything. but it didn’t work on Vscode Powershell.
but python was working in windows CMD. So I just reinstalled Vscode.

Go to preferences > settings in VS Code, search for "Code" extension and add this line in "settings.json" file:

{
  ...
  "python": "python"
  ...
}
Answered By: SujahathMSM

Try to install python from Microsoft stores

Answered By: Manasi Bhogale