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

12

29

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!")
Nursemaid answered 14/9, 2018 at 12:59 Comment(5)
What happens if you call python main.py?Valma
@Valma it says 'python' is not recognized as an internal or external command, operable program or batch fileNursemaid
That sounds like Python isn't installed correctly. This is strange because if py works, so should python. Have you read Using Python on Windows? You may need to manually run one or more command-line utilities to get the registry set up correctly. Although it's a bit dated, see this answer I once wrote for an example of what I am talking about.Valma
@Valma I found the problem, it needed a restart, but thanks for the useful informationNursemaid
After installing python, needs to restart both of terminal and VS Code if you already opened them. It worked for me without restarting computer.Nauseating
N
26

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.

Nursemaid answered 19/9, 2018 at 14:36 Comment(3)
jaja thats the solution, only restart the computerClannish
This did the trick for me too. If you just downloaded Code Runner you will need to restart and try again!Fortuneteller
I had to restart VS code after installing the python installer.Foust
V
5

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

Voroshilovsk answered 2/5, 2019 at 4:47 Comment(0)
E
3

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).

Exostosis answered 18/9, 2018 at 22:1 Comment(1)
in my version of python, it didn't provide that option, it turned out the problem was a restart issue, but thanks for the infoNursemaid
D
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.

Dishearten answered 17/6, 2020 at 8:29 Comment(0)
R
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.

Rapturous answered 18/5, 2020 at 18:52 Comment(0)
H
1

If you've tried all the above, and still no luck, click the "Run Python File" in the Code Runner drop-down menu in the top right corner.

enter image description here

Somehow it stuck at "Run Code" hence the "'python3' is not recognized as an internal or external command, operable program or batch file." message.

Even though I was using keyboard shortcuts to run the code, it still persisted.
Choosing the "Run Python File" unlocked the shortcuts, and all started working properly.

Hollywood answered 18/4, 2023 at 9:40 Comment(1)
'"Run Python File" in the Code Runner drop-down menu - Code Runner doesn't provide that menu, only the "Run Code" option. "Run Python File" is provided by the Python extension IIRC.Bessiebessy
P
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.

Perdu answered 7/4, 2020 at 11:18 Comment(0)
P
0

You have install anaconda navigator and launch VS code with this,then you can pip install in VS codeenter image description here

Pelagianism answered 11/7, 2024 at 13:13 Comment(1)
This doesn't answer OP's question.Joachim
C
-1

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.

Coopery answered 10/6, 2022 at 14:0 Comment(0)
H
-2

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

Hachmin answered 3/10, 2021 at 13:57 Comment(0)
E
-2

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

{
  ...
  "python": "python"
  ...
}
Exhibitive answered 8/3, 2023 at 7:21 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Senator
H
-2

Try to install python from Microsoft stores

Histogram answered 18/3, 2023 at 7:28 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Senator

© 2022 - 2025 — McMap. All rights reserved.