28

I want to able to open a command prompt at the folder which contains a python script and just type in the script name without the .py and watch it run.

Following the various tips on google to do this, I do:

  1. Add the python.exe to my path
  2. Add .py to PATHEXT
  3. Try to open the .py file in windows explored. When prompted with: What program do you want to open this? I navigate to my python.exe

What should happen is the python.exe should be added to the 'Open With' pop up, but it is not? I re try with the python.exe off my path. Same problem.

Note every time I set a path it is in the control panel. Version of python is 2.7. And to make things stranger, I can associate .py programs with pythonw - but this is no use, as I want the console.

Any help appreciated.

Piotr Dobrogost
  • 39,915
  • 36
  • 232
  • 353
dublintech
  • 15,727
  • 28
  • 81
  • 113
  • What exactly is happening behind the scenes when this is done? One should be able to do this from *Powershell* command line... – not2qubit Apr 01 '22 at 20:28

4 Answers4

41

Add .PY to PATHEXT as noted before

Then do:

assoc .py=Python.File
ftype Python.File=c:\Python27\python.exe "%1" %*

Adding python to the path isn't necessary to execute the script in a command prompt or double clicking in Explorer, only if you want to start an interactive python session or running the script with python yourscript.py

See http://docs.python.org/2/using/windows.html for more details.

Kim Gräsman
  • 7,268
  • 1
  • 27
  • 40
PabloG
  • 24,383
  • 10
  • 44
  • 59
  • 2
    If you have more than one version of Python installed (like 2.7.x and 3.4), you might want to use `ftype Python.File="C:\Windows\py.exe" "%1" %*`. – martineau May 30 '14 at 17:10
11

Modify the PATHEXT variable to include Python scripts. For example, here's mine:

PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY

You can do this every time you open a command console, or just modify your user global environment variables so that every instance of cmd.exe will include it.

kprobst
  • 15,555
  • 5
  • 30
  • 53
1

I was able to get it done using this application http://defaultprogramseditor.com/

dublintech
  • 15,727
  • 28
  • 81
  • 113
-1

Create a file named 'personalisedCommand.cmd' in a path folder with this inside:

@echo off python absolute/path/to/pythonScript

  • 1
    What advantage does this offer over the existing answers? It appears that a separate .cmd file would need to be created for each script you want to launch in this manner. – Jason Aller Apr 15 '18 at 22:53