Making Python scripts run on Windows without specifying ".py" extension

Question:

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.

Asked By: dublintech

||

Answers:

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.

Answered By: kprobst

Add .PY to PATHEXT as noted before

Then do:

assoc .py=Python.File
ftype Python.File=c:Python27python.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.

Answered By: PabloG

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

Answered By: dublintech

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

@echo off
python absolute/path/to/pythonScript

I tried removing .py extension from file.py name and then ran command python file python can understand the code without any extension for single file, but if you use modules then you need to use .py extension. Else you can add .py to path text in enviornment variables and run

assoc .py=Python.File

ftype Python.File=c:path to python.exe "%1" %*

Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.