9

I installed Python on my computer.

When I type python in the command prompt I get the following message:

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

But when I type py it seems to be working and I get the following:

Python 3.7.0 (v3.7.0, Jun 27 2018, 04:59:51) [MSC v.1914 64
bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license"
for more information.

Why is this happening?

FYI: I checked the path variable in environmental variables and I don't see any path to python installation.

But then how is visual code able to find the path to python.exe and able to run python code?

I am confused.

fcdt
  • 2,371
  • 5
  • 14
  • 26
SuperAadi
  • 627
  • 1
  • 6
  • 15
  • just add the path to the python exe – SuperStew Sep 17 '20 at 15:07
  • 1
    Take a look at that: https://renenyffenegger.ch/notes/Windows/dirs/Windows/py_exe#:~:text=C%3A%5CWindows%5Cpy.exe%20is,the%20Python%20Launcher%20for%20Windows.&text=py.exe%20helps%20to%20execute,to%20a%20specific%20Python%20executable. – Severin Pappadeux Sep 17 '20 at 15:08

3 Answers3

7

py is itself located in C:\Windows (which is always part of the PATH), which is why you find it. When you installed Python, you didn't check the box to add it to your PATH, which is why it isn't there. In general, it's best to use the Windows Python Launcher, py.exe anyway, so this is no big deal. Just use py for launching consistently, and stuff will just work. Similarly, if py.exe was associated with the .py extension at installation time, a standard shebang line (details in PEP linked above) will let you run the script without even typing py.

I don't know precisely what VSCode uses to find Python (using py.exe directly, using a copy of Python that ships with the editor, performing registry lookup, a config file that just says where to find it, etc.), but that's not really relevant to running your scripts yourself.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • Also, I have one more question related to environmental variables. How does Windows know when i type in 'py' to choose which of the many path variables to choose(it has path to .net,java,sql etc). Does it go through all the paths one by one? – SuperAadi Sep 17 '20 at 15:25
  • @SuperAadi: `py` isn't a Windows thing, it's installed with sufficiently modern Python installations. The PEP I linked contains the details on how it looks things up (it's not via the `PATH`), and it's only looking up Python (so .net, java, and sql aren't involved). You provide it command line switches to choose which version to use; when those switches are omitted, it just runs the most modern install. – ShadowRanger Sep 17 '20 at 15:37
  • 1
    @SuperAadi: It searches them, one by one from left to right in the `PATH`, until it finds something that matches. If you provide the full name, `py.exe`, it looks for that specifically, if you just type `py`, it uses the `PATHEXT` variable to check for anything that is named `py` plus one of the extensions in `PATHEXT` (e.g. `.exe`, `.com`, `.bat`, etc.). It's always using the first one found (`py.exe` exists because this "first found" behavior made it hard to use multiple versions of Python; only one could be first in the `PATH`, but they're all named `python.exe`, so choosing was a problem). – ShadowRanger Sep 17 '20 at 16:18
2

You could try execute in cmd:

where py

Output will be paths to the executable. E.g:

C:\Users\user>where python
C:\Users\user\Anaconda3\python.exe
C:\Users\user\AppData\Local\Microsoft\WindowsApps\python.exe

This helps you track down, which one is executed.

Alexandra Dudkina
  • 4,302
  • 3
  • 15
  • 27
2
  1. Go to system properties -> Advance

  2. Click environment variables

  3. Edit the 'PATH' variable

  4. Add 2 new paths 'C:\Python37' and 'C:\Python37\scripts'

  5. Run cmd again and type python.

It should work!

Or you can use the graphical way of Python Installer. Run the python installer and select Modify option enter image description here

Press next on next window if you need nothing changed.

Then next window Select Add Python to environment variables Continue the installation. enter image description here

Make sure you get a new terminal to test whether it works

Menuka Ishan
  • 5,164
  • 3
  • 50
  • 66