Python command not working in command prompt [duplicate]
Asked Answered
P

25

146

When I type python into the command line, the command prompt says python is not recognized as an internal or external command, operable program, or batch file. What should I do?

Note: I have Python 2.7 and Python 3.2 installed on my computer.

Peterkin answered 28/11, 2012 at 1:53 Comment(0)
P
110

It finally worked!!!

I needed to do things to get it to work

  1. Add C:\Python27\ to the end of the PATH system variable
  2. Add C:\Python27\ to the end of the PYTHONPATH system variable

I had to add these to both for it to work.

If I added any subdirectories, it did not work for some reason.

Thank you all for your responses.

Peterkin answered 28/11, 2012 at 2:51 Comment(7)
For newer versions of Python you can find it here: `C:\Users\UserNameHere\AppData\Local\Programs\Python\Python37`Mitchel
Jesus, why can't they do this for us at installation. This is why people prefer C#Tumbrel
PYTHONPATH was something I was missing.Lysippus
@Tumbrel you mean like this?Roguish
Why would you need to modify PYTHONPATH to run an executable from the command shell? Adding the directory to PATH should be enough to run python.exe. Then python may require the environment variable PYTHONPATH to be set to work correctly (supposedly to import modules from somewhere). Why would an executable no able to retrieve itself the directory it was launched from is another mystery, but at least it should be possible to run it. Too bad you didn't explain. And yes a proper install should deal with this sort of things, not relying on the user being a specialist in operating systems.Loculus
Just to have it mentioned for future reference, I had python 3.7 installed with the installer from python.org and then uninstalled it and installed python 3.9 But when I typed python nothing happend, also no error message. I found out that in %USERPROFILE%\AppData\Local\Microsoft\WindowsApps Windows created a python.exe with 0kb which was in my path before the actual installation. - > may be worth checking when there is no "was not reckognized..." but if it's still not working.Biondo
Thanks @robin. I have the same problem with 0 sized python.exe & python3.exe in my WindowsApps folder. I could pinpoint the problem with running which python in CMD.Hone
S
142

I have installed the latest Python for Win10 from Releases for Windows. Just typing py in the Command Prompt Window starts Python.

Microsoft Windows [Version 10.0.15048]
(c) 2017 Microsoft Corporation. All rights reserved.

C:\Users\sg7>py
Python 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>`enter code here`

Testing:

>>> print("hello!")
hello!
>>>

Please be aware that in my case Python was installed in C:\Users\sg7\AppData\Local\Programs\Python\Python36> directory

C:\Users\sg7\AppData\Local\Programs\Python\Python36>dir
 Volume in drive C is Windows7_OS
 Volume Serial Number is 1226-12D1

 Directory of C:\Users\sg7\AppData\Local\Programs\Python\Python36

08/05/2018  07:38 AM    <DIR>          .
08/05/2018  07:38 AM    <DIR>          ..
12/18/2017  09:12 AM    <DIR>          DLLs
12/18/2017  09:12 AM    <DIR>          Doc
12/18/2017  09:12 AM    <DIR>          include
12/18/2017  09:12 AM    <DIR>          Lib
12/18/2017  09:12 AM    <DIR>          libs
10/03/2017  07:17 PM            30,334 LICENSE.txt
10/03/2017  07:17 PM           362,094 NEWS.txt
10/03/2017  07:15 PM           100,504 python.exe
10/03/2017  07:12 PM            58,520 python3.dll
10/03/2017  07:12 PM         3,610,776 python36.dll
10/03/2017  07:15 PM            98,968 pythonw.exe
08/05/2018  07:38 AM           196,096 Removescons.exe
08/05/2018  07:38 AM            26,563 scons-wininst.log
08/05/2018  07:38 AM    <DIR>          Scripts
12/18/2017  09:12 AM    <DIR>          tcl
12/18/2017  09:12 AM    <DIR>          Tools
06/09/2016  11:53 PM            87,888 vcruntime140.dll
               9 File(s)      4,571,743 bytes
              10 Dir(s)  20,228,898,816 bytes free

When I am at C:\Users\sg7> directory level python can be invoked by typing AppData\Local\Programs\Python\Python36\python

C:\Users\samg>AppData\Local\Programs\Python\Python36\python
Python 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

Newer Python 3.7 will be installed at: C:\Users\YourUserNameHere\AppData\Local\Programs\Python\Python37

If you wish you can add to your path environment variable: %USERPROFILE%\AppData\Local\Programs\Python\Python36

Sakmar answered 18/12, 2017 at 13:50 Comment(4)
This works for me as well. Calling python and then checking sys.executable gives me C:\\Python27\\python.exe. Calling py, I get C:\\Users\\{user}\\AppData\\Local\\Continuum\\anaconda3\\python.exe. Anaconda is in my PATH but Python27 isn't. If someone has insight into why this is the way it is, I'd very much appreciate it! EDIT: tried running py -i {filename} and it ran in Python 2.7. Grr.Francinefrancis
+1 although I have to say this shortcut is pathetic. Now I want to use pip and have no idea what I should type.Engelhart
I'm a Win 10 user with Python 3.8 and this worked for me, as well. I was using "Python file.py" and nothing was happening. I typed "py file.py" and it worked. Thanks!Safko
The funniest thing. Yesterday python was the command to invoke Python. I have 3.10.6. Today, I woke up to python not working. I repaired it, I modified it, updated PATH variables, rebooted multiple times. Python was installed, the terminal was there so, idk what was going on until I read this, and indeed now it is py to invoke. Which is really weird.Laclos
P
110

It finally worked!!!

I needed to do things to get it to work

  1. Add C:\Python27\ to the end of the PATH system variable
  2. Add C:\Python27\ to the end of the PYTHONPATH system variable

I had to add these to both for it to work.

If I added any subdirectories, it did not work for some reason.

Thank you all for your responses.

Peterkin answered 28/11, 2012 at 2:51 Comment(7)
For newer versions of Python you can find it here: `C:\Users\UserNameHere\AppData\Local\Programs\Python\Python37`Mitchel
Jesus, why can't they do this for us at installation. This is why people prefer C#Tumbrel
PYTHONPATH was something I was missing.Lysippus
@Tumbrel you mean like this?Roguish
Why would you need to modify PYTHONPATH to run an executable from the command shell? Adding the directory to PATH should be enough to run python.exe. Then python may require the environment variable PYTHONPATH to be set to work correctly (supposedly to import modules from somewhere). Why would an executable no able to retrieve itself the directory it was launched from is another mystery, but at least it should be possible to run it. Too bad you didn't explain. And yes a proper install should deal with this sort of things, not relying on the user being a specialist in operating systems.Loculus
Just to have it mentioned for future reference, I had python 3.7 installed with the installer from python.org and then uninstalled it and installed python 3.9 But when I typed python nothing happend, also no error message. I found out that in %USERPROFILE%\AppData\Local\Microsoft\WindowsApps Windows created a python.exe with 0kb which was in my path before the actual installation. - > may be worth checking when there is no "was not reckognized..." but if it's still not working.Biondo
Thanks @robin. I have the same problem with 0 sized python.exe & python3.exe in my WindowsApps folder. I could pinpoint the problem with running which python in CMD.Hone
W
55

The video was very useful.

  1. Go to system properties -> Advance ( or type "system env" in start menu.)
  2. Click environment variables
  3. Edit the 'PATH' variable
  4. Add 2 new paths 'C:\Python27' and 'C:\Python27\scripts'
  5. Run cmd again and type python.

it worked for me

Weighted answered 4/8, 2016 at 11:52 Comment(0)
M
51

Rather than the command "python", consider launching Python via the py launcher, as described in sg7's answer, which runs your latest version of Python (or lets you select a specific version). The py launcher is enabled via a check box during installation (default: "on").

Nevertheless, you can still put the "python" command in your PATH, either at "first installation" or by "modifying" an existing installation.


First Installation:

Checking the "[x] Add Python x.y to PATH" box on the very first dialog. Here's how it looks in version 3.8: enter image description here

This has the effect of adding the following to the PATH variable:

C:\Users\...\AppData\Local\Programs\Python\Python38-32\Scripts\
C:\Users\...\AppData\Local\Programs\Python\Python38-32\

Modifying an Existing Installation:

Re-run your installer (e.g. in Downloads, python-3.8.4.exe) and Select "Modify". Check all the optional features you want (likely no changes), then click [Next]. Check [x] "Add Python to environment variables", and [Install]. enter image description here

Midlothian answered 3/1, 2019 at 21:0 Comment(2)
I cant see advanced optionsCortney
I've overhauled the answer to distinguish between first install and re-installation, and also to mention the "py" launcher. And, I've updated it to 3.8Midlothian
D
36

In case someone else has the same issue, if you have already added to PATH and it (C:\Users\pete.kirkham\AppData\Local\Programs\Python\Python39\python) works if you run with a full path, on Win 10 the command line command python is by default aliased to the Windows store.

Go to settings > App execution aliases and disable the alias for python that runs App installer instead.

More details can be found in this question CMD opens window store when I type python

(presumably if your organisation doesn't disable the Windows Store and you install via the Store it disables this when you install, but installing via download from python.org does not)

enter image description here

Doralynne answered 5/3, 2021 at 9:41 Comment(3)
Incredible... I found this, and to be honest this aliases is really something microsoft should avoid. If I add something to the path I expect it to get used if I call it, and not something completely differently happening.Ratoon
I can't up @Ratoon enough. Came here for same reason. How incredibly stupid. No wonder why folks continue to badmouth Microsoft. Incredibly draconian behavior.Shoemake
This feature works for a bit, but then it gets reset. I find that when Python presents the "Python was not found", it is necessary to go into the "manage app execution aliases" again and turn it off. Apparently there are many Microsoft updates that deliberated turn it back on. Would be great if more folks up this so it floats to the top.Diomedes
P
35

In Windows 7 python start command in command prompt is

c:\>python3

but in Windows 10 python start command in command prompt is

  C:\>py
Python 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>


C:\>py --version
Python 3.6.3

C:\>

But in Windows 10 python3 syntax not work also not given any error.

Django start command also uses py instead of python3.

d:\>py manage.py runserver

But in Windows 11 after the installation path is not added, sometimes we need to add using Environment Variable in the User variable section.

limelightnss.com

Privilege answered 15/12, 2019 at 6:1 Comment(2)
In Windows 7, to start python from the cmd prompt you need not to type python3, python is enough. There is a python.exe file in the install directory, at least for version 3.8.6 I use.Loculus
I'm on Windows 10. In cmd window python is not recognized but py works.Beamends
C
16

This working for me :

PS C:\Users\MyUsername> py -V

Python 3.9.0
Chare answered 4/2, 2021 at 7:39 Comment(0)
B
15

Python 3.10 uses py and not python.

Try py --version if you are using this version.

Brooklime answered 24/3, 2022 at 15:39 Comment(0)
F
13

They gave us a script to do this for us already

C:\Users\hUTBER\AppData\Local\Programs\Python\tools\scripts\win_add2path.py

You'll need to make sure that you close and open the cmd otherwise it won't have the new path there.

If you can't find this script these are the paths that it will add and I had to add manually in the end.

C:\Users\hUTBER\AppData\Local\Programs\Python\Python35
C:\Users\hUTBER\AppData\Local\Programs\Python\Python35\Scripts

Where mine and now python works in the cmd

Flutter answered 24/11, 2015 at 13:56 Comment(3)
Closing and reopening the cmd prompt did it!Auvergne
Don't forget to upvote the answer :D If it has more votes more people will see it and try the same thingFlutter
I did vote it up. I am surprised that I am the only to vote up your comment. Sometimes it is as simple as closing and reopening your terminal.Auvergne
N
7

Even after following the instructions from the valuable answers above, calling python from the command line would open the Microsoft Store and redirect me to a page to download the software.

I discovered this was caused by a 0 Ko python.exe file in AppData\Local\Microsoft\WindowsApps which was taking precedence over my python executable in my PATH.

Removing this folder from my PATH solved it.

Nawab answered 25/2, 2020 at 15:0 Comment(1)
You can downgrade it in PATH as well, instead of deleting itSignalment
W
5

Kalle posted a link to a page that has this video on it, but it's done on XP. If you use Windows 7:

  1. Press the windows key.
  2. Type "system env". Press enter.
  3. Press alt + n
  4. Press alt + e
  5. Press right, and then ; (that's a semicolon)
  6. Without adding a space, type this at the end: C:\Python27
  7. Hit enter twice. Hit esc.
  8. Use windows key + r to bring up the run dialog. Type in python and press enter.
Westfahl answered 28/11, 2012 at 2:7 Comment(0)
S
5

Just a few comments:

  1. Don't set PYTHONPATH if all you want is to get Python on the PATH. The PYTHONPATH environment variable tells Python where to look for modules to import. Setting it to C:\Python27\ will not accomplish anything useful, although it's probably harmless.

  2. Modifying environment variables (including PATH) from the "Edit System Variables" has no effect on already running processes. This means you have to re-launch cmd.exe for the changes to work. A reboot, however, is not required.

  3. When modifying the PATH, also add the Scripts subdirectory. Or, to put it in other words (and using the previous example): add ;C:\Python27;C:\Python27\Scripts. This will allow you to run scripts like easy_install, pip, virtualenv or sphinx from the command line - once you install those, that is. This is about as UNIX-y as it gets for Windows. (N.B. The Scripts subdirectory is not present after a clean install of Python, but will be created when needed.)

  4. Don't put any additional Lib or DLL directory on the PATH. There's no need, and it might do harm.

  5. If you have installed multiple versions of Python (which isn't all that uncommon) you might be better off not putting any of them on the PATH but instead create different shortcuts to cmd.exe for the different versions which set the PATH for each version. You might also be interested in PEP-397.

Sturgeon answered 28/11, 2012 at 19:48 Comment(0)
E
5

I am probably the most novice user here, I have spent six hours just to run python in the command line in Windows 8. Once I installed the 64-bit version, then I uninstalled it and replaced it with 32-bit version. Then, I tried most suggestions here, especially by defining path in the system variables, but still it didn't work.

Then I realised when I typed in the command line: echo %path%

The path still was not directed to C:\python27. So I simply restarted the computer, and now it works.

Eupatrid answered 2/4, 2015 at 0:8 Comment(0)
T
4

You have to add the python executable in your SYSTEM PATH, do the following, My Computer > Properties > Advanced System Settings > Environment Variables > Then under system variables I create a new Variable called "PythonPath". In this variable I have "C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk;C:\other-foolder-on-the-path".

enter image description here

Trebizond answered 28/11, 2012 at 2:1 Comment(7)
This didn't work for me, I am not sure what is wrong with my computer.Peterkin
@Rohit Rayudu Try adding C:\Python27 as one of the paths. It should work. And check that your python directory is named correctly.Trebizond
Try rebooting, it should work. Other than that I have no idea.Trebizond
Ok thanks for the help, I will reboot my computer, I really really hope it works. Ever since I uninstalled Google App Engine, this has been happening to me, does that give you any other ideas of what might be wrong?Peterkin
If it doesn't work after rebooting, go back and make sure the environment variable changes you made are still there.Bhayani
If that doesn't work just make a batch file or you can just type in the full path, C:\Python27\python.exeTrebizond
Thank you all so much. I needed to add it to both the PYTHONPATH and PATH system variables for it to work.Peterkin
B
3

Here's one for for office workers using a computer shared by others.

I did put my user path in path and created the PYTHONPATH variables in my computer's PATH variable. Its listed under Environment Variables in Computer Properties -> Advanced Settings in Windows 7.

Example:

C:\Users\randuser\AppData\Local\Programs\Python\Python37

This made it so I could use the command prompt.

Hope this helped.

Blamed answered 29/9, 2018 at 20:42 Comment(0)
C
2

Seems like the python executable is not found in your PATH, which defines where it is looking for executables. See the official instructions for instructions on how to get the python executables in your PATH.

Congest answered 28/11, 2012 at 2:0 Comment(0)
M
2

You need to add python to your PATH. I could be wrong, but Windows 7 should have the same cmd as Windows 8. Try this in the command line. Using setx permanently makes changes to you PATH. Note there are no equal signs, and quotes are used.

setx PATH "%PYTHONPATH%;C:\python27"

Set the c:\python27 to the directory of the python version you'd like to run from the typing python into the command prompt.

Motherless answered 28/11, 2012 at 2:1 Comment(5)
I tried this, it works, kind of. if I do this, it works temporarily, but if I close the command prompt and then reopen it and type python again, it stops working. Is this supposed to happen/do I have to insert this line, every time?Peterkin
@RohitRayudu: Yes, it's only temporary if you do it this way -- which is why other answers suggested changing it via System Properties or Control Panel. The good news is the fact that this work seems to indicate it's a PATH settings problem.Bhayani
How do I fix such a PATH settings problem, @BhayaniPeterkin
@enginefree's answer sounds like should permanently change the PATH.Bhayani
@RohitRayudu - setx is the permanent alternative to set, I made changes in my answer.Motherless
M
2

Add the python bin directory to your computer's PATH variable. Its listed under Environment Variables in Computer Properties -> Advanced Settings in Windows 7. It should be the same for Windows 8.

Marika answered 28/11, 2012 at 2:2 Comment(0)
S
2

Just go with the command py. I'm running python 3.6.2 on windows 7 and it works just fine.
I removed all the python paths from the system directory and the paths don't show up when I run the command echo %path% in cmd. Python is still working fine.
I ran into this by accidentally pressing enter while typing python...

EDIT: I didn't mention that I installed python to a custom folder C:\Python\

Styria answered 30/8, 2017 at 22:28 Comment(1)
I had to use py instead of pythonEras
V
2

In my scenario, when I execute py -V it will show me the python version,

but python -V will show me Python was not found;

Answers above show us that we may need to set environment variable for python. But I already set the environment variable.

Finally, when I try to execute where.exe python command, it showed me with 2 python.exe, one of them is in path \AppData\Local\Microsoft\WindowsApps which is also set as environment, so I delete this python.exe file, then it worked.

enter image description here

Vasquez answered 3/8, 2022 at 12:49 Comment(0)
B
1

When you add the python directory to the path (Computer > Properties > Advanced System Settings > Advanced > Environmental Variables > System Variables > Path > Edit), remember to add a semicolon, then make sure that you are adding the precise directory where the file "python.exe" is stored (e.g. C:\Python\Python27 if that is where "python.exe" is stored). Then restart the command prompt.

Burks answered 21/1, 2016 at 21:17 Comment(0)
M
1

If you are working with command prompt and if you are facing the issue even after adding python path to system variable PATH.

Remember to restart the command prompt (cmde.exe).

Myrlmyrle answered 26/9, 2017 at 20:30 Comment(0)
G
1

I wanted to add a common problem that happens on installation. It is possible that the path installation length is too long. To avoid this change the standard path so that it is shorter than 250 characters.

I realized this when I installed the software and did a custom installation, on a WIN10 operation system. In the custom install, it should be possible to have Python added as PATH variable by the software

Gunderson answered 7/4, 2020 at 11:12 Comment(1)
This is very true. I had the same problem and changing the path to shorter one solved it. Thank you!Quadruplicate
H
1

Install python then set environment variable and restart system to be applied the changes. Do as follows : enter image description here

enter image description here

Headreach answered 24/4, 2023 at 14:58 Comment(0)
C
0

None of these actually worked for me. What you needed to do to really have Python recognized within it's path, is to download the latest version of it only from this website and not other website: https://www.python.org/downloads/

But be careful while installing; the default installation is set not to add Python's path to the Environmental Variables in the Control Panel if you have a Windows computer, but you should change the setting so that the installation does it, and it will all be done by itself.

Cioban answered 27/3, 2018 at 20:19 Comment(1)
You said "is to download the latest version of it only from this website and not other website: python.org/downloads", but you never said what the "this website" is.Wheaten

© 2022 - 2024 — McMap. All rights reserved.