How do I make python scripts executable?
Executing PY files #
On Windows 2000 and XP, the standard Python installer already associates the .py extension with a file type (Python.File) and gives that file type an open command that runs the interpreter (D:\Program Files\Python\python.exe “%1” %*). This is enough to make scripts executable from the command prompt as ‘foo.py’. If you’d rather be able to execute the script by simply typing ‘foo’ with no extension you need to add .py to the PATHEXT environment variable.
On Windows NT, the steps taken by the installer as described above allow you to run a script with ‘foo.py’, but a longtime bug in the NT command processor prevents you from redirecting the input or output of any script executed in this way. This is often important.
The incantation for making a Python script executable under WinNT is to give the file an extension of .cmd and add the following as the first line:
@setlocal enableextensions & python -x %~f0 %* & goto :EOF
Creating EXE files #
You can also use deployment tools to wrap Python scripts or entire Python applications in EXE files. For entire applications, you can use py2exe, among others:
Another tool is exemaker, which takes a PY file and generates an EXE wrapper for it. When you run the EXE, it automatically locates a suitable Python interpreter, and uses that to execute the PY file.