Welcome, guest | Sign In | My Account | Store | Cart

This recipe contains information on wrapping your python scripts in batch files on win2k. This will enable your scripts to appear as if they are batchfiles and get automatically picked up from the path.

Python, 14 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
@echo off
rem = """
Do any custom setup like setting environment variables etc if required here ...

python -x "%~f0" %1 %2 %3 %4 %5 %6 %7 %8 %9
goto endofPython """

# Your python code goes here ..
   
if __name__ == "__main__":
	print "Hello World"

rem = """
:endofPython """

It is very helpful to have python scripts along with other scripts which are usually available through the path and invoke them just like other scripts. This gives you the power of python and the ease of invocation of a batch script.

Wrap your python code in a .bat file with code shown in the recipe and you are ready !

Note that this method works with the cmd shell. For those using 4nt - you will have to substitute "%~f0" with "%_batchname"

4 comments

Marcel Luethi 21 years, 3 months ago  # | flag

Small "bug" When running there's an error message on my XP commandline caused by the two lines below:

rem = """
Do any custom setup like setting environment variables etc if required here ...

which should be on the same line:

rem = """ Do any custom setup like setting environment variables etc if required here ...

IMHO: Very elegant way to start a script... - nice recipe!

Kevin Altis 21 years, 3 months ago  # | flag

instead add .py and .pyw to PATHEXT. There is a much simpler way. Simply add .py and .pyw to the PATHEXT environment variable on Windows NT, 2000, and XP (possibly Win9x and ME too, but I can't test that).

Open the System Control Panel, select the Advanced tab and then click the Environment Variables... button to bring up the dialog. PATHEXT is listed under the System variables. Once you've made the change, if you type set and press return in the command shell you should see your change:

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

Now if you have a program called hello.py in one of the directories on your PATH, you can invoke it by just typing hello and return.

Note that you will won't be able to select a .py or .pyw file in the Explorer to open files of a particular extension. In order to do that, you'll need to use a variation of the instructions for associating .py and .pyw files with the codeEditor found at:

http://wiki.wxpython.org/index.cgi/PythonCardEditor

Just substitute your program for the codeEditor, and the extensions such as .txt, or .jpg, etc. you want to open.

Chris Arndt 18 years, 9 months ago  # | flag

Caveat. One thing to be aware of, if you follow the advice of adding .py;.pyw to PATHEXT:

You can call python scripts that are on the PATH or in the current directory by just typing the script name without the extebsion.

BUT: if you add the extension, the default action for that extension is called.

So if, for example, you followed the above mentioned tip and configured PythonCardEditor to be opened, when a .py file is double-clicked in the explorer, then PythonCardEditor will also be opened when you type in the name of a Python file with extension at the command prompt!

At least, my Win2k box acts like this.

Dobedani 15 years, 6 months ago  # | flag

Hi folks! Why make things so difficult? It is possible to have Python scripts invoked from VBScript. I have posted some code of general use, for invoking Python scripts from VBScript here: http://uk.blog.360.yahoo.com/blog-fk158Uc1dKTZFSiMvzMH.A--?cq=1 HTH Regards, Dobedani

Created by S Kalyan on Tue, 24 Dec 2002 (PSF)
Python recipes (4591)
S Kalyan's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks