0

I've been using Python for some time now, but I have never been able to properly run it from the Windows command line. The error shown is:

C:\Windows\system32>python

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

I've tried to solve the problem many times. I understand it's a matter of editing the environment variables, but this hasn't fixed the problem. My System Path variable is currently

C:\Python27;C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk

This is the correct location of Python in my directory. I've tried adding this to my User Path, and I've tried creating a PYTHONPATH variable containing them.

I should note that running python.exe does work.

C:\Windows\system32>python.exe

Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win 32 Type "help", "copyright", "credits" or "license" for more information.

I've tried a variety of solutions to no avail. Any help is greatly appreciated.

acal2112
  • 13
  • 1
  • 4
  • If `python.exe` works, but `python` doesn't, it sounds like you've messed up the `PATHEXT` environment variable. It specifies the extensions to try when you don't specify one in the command. Mine looks like `PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY;.PYW;.PYC;.PYO;.tcl`. – Mark Tolonen Oct 22 '14 at 05:28
  • That could likely be the case, but my PATHEXT includes EXE. I have PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY;.PYW;.PYC;.PY??O – acal2112 Oct 22 '14 at 05:55
  • You shouldn't have any of these directories in the system search `PATH`: `C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk`. Python's import system looks in these directories for modules. They're pre-configured, so don't set them in `PYTHONPATH` either; that would cause problems if Python 3 is also installed. – Eryk Sun Oct 22 '14 at 06:22
  • Thanks for the help everyone. Path extensions was the issue. I also used the answer from [this question](http://stackoverflow.com/questions/9037346/making-python-scripts-run-on-windows-without-specifying-py-extension) in my solution. – acal2112 Oct 22 '14 at 20:51

2 Answers2

4

Install Python 3.3 or later. It comes with a utility called the Python Launcher (py.exe), that is installed in the Windows directory and is already in the path. With it, you can launch any version of Python you have installed.

The default Python to run can be overridden with the PY_PYTHON variable.

Below is a Demo. I have three versions of Python installed:

C:\>set PY_PYTHON
PY_PYTHON=3.3

C:\>py
Python 3.3.5 (v3.3.5:62cf4e77f785, Mar  9 2014, 10:35:05) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> ^Z

C:\>py -2
Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> ^Z

C:\>py -3
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> ^Z

Also, if you add a special line to the top of Python files, you can choose which version of Python the script will run under:

#!python3

Full details here: https://docs.python.org/3.4/using/windows.html#python-launcher-for-windows

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
1

One quick solution to those who are still struggling with environment variable setup issue. Just Uninstall the existing python version and reinstall it make sure to enable checkbox as "Add Python 3.10 to PATH to the environment variable.

Bharti Jha
  • 141
  • 1
  • 5