Running Python Code in Windows Batch File Trick

I found this really neat bit of .bat file magic that will let you save your python script code in a .bat file and run it in windows just like any other script. The nice thing about this is that you don’t have to create a separate “launch.bat” file with one “start python script.py” line in it.

This makes running python scripts in Windows more like it is on a Linux/Mac where you can easily add a #!/usr/bin/env python line to the script and run it directly.

Here’s the bit of tricky batch file magic that does it:

@setlocal enabledelayedexpansion && python -x "%~f0" %* & exit /b !ERRORLEVEL!
#start python code here
print "hello world"

The way it works is that the first line of the file does two different things.

  1. starts python interpreter passing the name of the file in, and the -x option will tell it to skip the first line (containing .bat file code)
  2. When python finishes the script exits.

This nifty trick makes it much nicer for writing admin scripts with python on Windows.

Update: fixed to properly pass command line arguments (%* argument passes through the command line arguments for the bat file to python)

Technorati Tags: , , , , ,



RSS feed | Trackback URI

10 Comments »

Comment by Jim
2009-08-22 03:13:19

Nice trick. How would you recommend passing in invocation arguments from the command line? I’d like to be able to use sys.argv from within my .bat embedded python program.

thanks

 
Comment by Matt Warren
2009-08-22 08:55:11

I updated the batch file line to pass through the command line arguments.

Example Usage:

$ echo.bat test
test

Here’s what echo.bat looks like:

@setlocal enabledelayedexpansion && python -x "%~f0" %* & exit /b !ERRORLEVEL!
#start python code here
import sys
print sys.argv[1]
 
Comment by Brian
2009-08-22 09:40:00

Why not just register .py files as runnable?

You’d have to do this for every python file you want to launch, it prevents you from using the same file for both script and module, and it seems needlessly complex when you can accomplish the same goal for every python file simply by registering the .py extension (already done by the installer) and extending the PATHEXT environment variable:

c:\> echo import sys; print ' '.join(sys.argv[1:]) >test.py
c:\> SET PATHEXT=%PATHEXT%;.py
c:\> test hello world
hello world
 
Comment by Greg
2009-08-22 23:32:42

How is this useful? On my machines, I simply associate .py extensions to Python. Done. Runs when I doubleclick it now.
On others’ machines that don’t have Python, I compile my script using Py2Exe. Runs when they doubleclick it now.

Comment by Greg
2009-08-22 23:37:11

Don’t get me wrong – much kudos to you for this cleverness. I just have zero use for this in a production environment.

 
 
Comment by Matt Warren
2009-08-25 11:44:37

quite right. you can just set Windows to associate the .py file with python. what this .bat file trick accomplishes is to make it familiar to users that are not used to running .py files. Now they get a .bat file which they know they can click to run a program. It’s just one less thing that you have to explain to users of your scripts.

There are lots of people in my office that if I sent them a python script I would get a “What do I do with this?” type response. But they would know what to do with a .bat file.

 
2011-08-15 04:19:52

[...] Running Python Code in Windows Batch File Trick [...]

 
Comment by Youpsla
2011-12-02 21:18:43

It’s not the same using the trick and addig.py in PATHEXT.

For example, I use pycron to launch a python script wich import modules.

Without the trick, I’ve an import module error at the first import.

I don’t know internallt how it works but it works !!

Thanks for the trick

 
Comment by Youpsla
2011-12-02 21:19:19

It’s not the same using the trick and addig.py in PATHEXT.

For example, I use pycron to launch a python script wich import modules.

Without the trick, I’ve an import module error at the first import.

I don’t know internallt how it works but it works !!

Thanks for the trick

 
Comment by Youpsla
2011-12-02 21:19:41

It’s not the same using the trick and addig.py in PATHEXT.

For example, I use pycron to launch a python script wich import modules.

Without the trick, I’ve an import module error at the first import.

I don’t know internallt how it works but it works !!

Thanks for the trick

 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight=""> in your comment.