0

Possible Duplicate:
How to make python scripts executable on Windows?
Set up Python on Windows to not type python in cmd

I would like to be able to execute a Python script with one command (i.e. filename -p parameter1) without having to type python filename.py -p parameter1. I tried adding the Windows shebang line; however, when I execute .\filename.py in the command line, it opens a new shell altogether just long enough for the output to flash on the screen and then the window closes. Is there any way to run that script and output to the current console?

Additionally, is there anything I can do to get rid of the need for the .py extension and make it callable from any directory?

Community
  • 1
  • 1
weberc2
  • 7,423
  • 4
  • 41
  • 57

2 Answers2

2

Set the PATHEXT environment variable to include the .PY extension. See http://www.voidspace.org.uk/python/articles/command_line.shtml#pathext .

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
0

The shebang line is not honored in Windows, although I still put it in my code for reference of which version of Python I'm using with each script I create.

I recommend making a BAT file for each PY script you make in Windows. Given that multiple versions of Python can be installed on the same system, this helps avoid version conflicts and will do what you want:

Given "filename.py", make "filename.bat". Assuming python27, the BAT file will contain:

@REM Need to unset "PYTHONPATH=C:\Program Files\ArcGIS\bin"
@REM so that "import zlib" can properly work.
@set PYTHONPATH=
@"C:\python27\python.exe" "C:\fullpath\to\my\file\filename.py" %*

You only need to use the last line if you want to keep it simple. Note that the @ sign makes the BAT file not echo the line. The %* tells the BAT file to pass all the arguments to your python script. As long as the BAT file is in your path, you can call it from anywhere as "filename.bat" or "filename".