How can I create a bat file to run a python file, specifically containing pygame.
-
3Figure out how you would do it from the command prompt, and put that in a batch file. – Ignacio Vazquez-Abrams Apr 11 '11 at 01:51
-
2If you're looking for something people can just double-click, then [py2exe](http://www.py2exe.org/) is a good solution. – Adam Rosenfield Apr 11 '11 at 02:06
4 Answers
Simple.
Just put the following as the very first line of the batch file:
python -x %0 %* &goto :eof
The rest of the batch file is the Python program. Here is a complete example:
python -x %0 %* &goto :eof
import sys
print "this is a batch file"
sys.exit()
First of all the & is a delimiter and allows the first line to contain two separate batch commands. The first one runs Python and the second command skips over the rest of the batch file which is Python code, not batch language.
The -x
is a Python option which skips the first line of the file, therefore this special line is only processed as a batch file, and not by the Python interpreter. %0
and %*
are two special batch file variables that represent the name of the batch file itself and any arguments given on the line when the batch file was invoked.
I have tried this as bot a .bat
and a .cmd
file and the only caveat is that you need to invoke the file with the full name including the extension. See a similar question on how to use Jscript in batch files.

- 1
- 1

- 31,973
- 6
- 70
- 106
-
FWIW, a hack which allows to call without extension: `@echo off& python -x %0".bat" %* &goto :eof`. – eat Apr 11 '11 at 08:52
-
What a hack. Otherwise acceptable, but at least two problems: it forces your python files to be named to end `.bat`! And: line numbers in error messages are now off by 1, for beginners real nice surprise! Thanks – eat Apr 11 '11 at 09:10
-
This is not for use in writing code, but for use in deploying already written and TESTED code. – Michael Dillon Apr 11 '11 at 15:18
-
But clearly OP is novice related to this issue. No need to make the issue (potentially) any more complicated. Also I don't know how the more seasoned ones really takes your approach, but just to speculate I expect that your approach won't be adopted with majority. Thanks – eat Apr 11 '11 at 16:32
-
`goto :eof` could be replaced with `exit /b [optional error level]`. Just to avoid using `goto` first time for last 20 years ;) – Alex Musayev Jun 03 '12 at 13:55
-
If you use `"%~f0"` instead of `%0` you get the fully qualified path to the script, which might be necessary if you want to run it from a directory other than the one that is current when you're executing it. – dash-tom-bang Sep 21 '16 at 20:05
All a bat file is is a plain text file (I mean PLAIN text, not MS word) with a list of commands.
For example, if you would open your game from the command line like this:
python MyGame.py
Then all you have to do is create a file containing exactly that. Change the extension to .bat
and you're done.

- 15,418
- 12
- 44
- 65
-
I tried that and got the following message: 'python' is not recognized as an internal or external command, operable program or batch file. It also changed my program name to end .pyw.py – Paul Ronjak Apr 11 '11 at 06:18
-
@Paul Ronjak: Is the `directory`, where your `python.exe` is located, listed in your `path`? If not, you must add it there. Thanks – eat Apr 11 '11 at 09:21
-
That still does not work. I've got C:\........pythonfile.py and then 'pause' on the next line. It just says the file name is not a recognizable command. – Paul Ronjak Apr 11 '11 at 15:41
-
1Never mind, I have figured this out. All I had to do was add a file path to my Python program before the file name. – Paul Ronjak Apr 11 '11 at 15:48
-
@Paul Ronjak: So it still seems that your `python.exe` is not in your `path`. Also have you tried whether the command `> MyGame.py` just works in your system. Thanks – eat Apr 11 '11 at 16:36
Type command to cd
the directory, e.g. cd c:/users/me/Desktop
.
Then on the next line type mygame.py
.
Your code should look like:
cd c:/users/me/Desktop
mygame.py

- 1,373
- 1
- 18
- 26

- 1
- 1
- 1
-
Welcome to SO! Nice of you to help out, but you should explain what your answer adds over existing ones. Otherwise its quite redundant, isn't it? – Hermann Döppes Jan 08 '17 at 17:51