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.
- 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)
- 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)


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
I updated the batch file line to pass through the command line arguments.
Example Usage:
Here’s what echo.bat looks like:
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:
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.
Don’t get me wrong – much kudos to you for this cleverness. I just have zero use for this in a production environment.
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.
[...] Running Python Code in Windows Batch File Trick [...]
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
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
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