How can I create a simple BAT file that will run my python script located at C:\somescript.py?
16 Answers
c:\python27\python.exe c:\somescript.py %*

- 33,605
- 18
- 74
- 111
-
2Hi MK- When I open Notepad, paste your line in there and save it and try to run it through windows scheduler, the commandline window opens for a split second and then closes. I'm not seeing a python27 folder in C:\ is that an issue? – Josh Dec 31 '10 at 17:01
-
In case it helps, I'm using Windows Server 2003 SP2 on 64 bit environment – Josh Dec 31 '10 at 17:02
-
6Well, you do need to install python first. Go to www.python.org and download version 2.7 – MK. Dec 31 '10 at 17:12
-
4If you'd prefer to never have the command window show, you can use the pythonw executable by exchanging `python.exe` with `pythonw.exe`. – arboc7 Jan 01 '11 at 03:04
-
3@alwbtc Are you calling your script as `pythonw myscript.py` or just `./myscript.py`? If the latter, change the extension to `.pyw` and try again. – arboc7 Sep 11 '13 at 21:00
-
If you want the window to stay open, start with cmd /k. – Noumenon Jul 27 '15 at 13:30
-
is it possible to give the whole code, instead of the file name?? – It's a trap Jul 25 '16 at 09:59
-
I have seen other people do c:\python27\python.exe c:\somescript.py %1 but here it is %*. Can someone explain the difference? – hfrog713 Aug 28 '17 at 19:12
-
3%* passes all arguments, %1 only the first one. – MK. Aug 29 '17 at 14:05
-
@arboc7 : I tried using all 3 combinations but none of them worked for me, which is the syntax you are pointing to? (1) pythonw ABC.py (2) python ABC.pyw and (3) pythonw ABC.pyw – Garima Tiwari May 03 '18 at 10:09
-
@GarimaTiwari I need a little more context about what you're trying to run and in what environment to be sure. (1) should work if you add ".exe" after "pythonw" - "pythonw.exe ABC.py" – arboc7 May 04 '18 at 22:46
-
Would this approach affect logging file? I used the logging module when running it in IDE it works, but with .bat, nothing is created. – Donald Li Jan 28 '20 at 19:09
Open a command line (⊞ Win+R, cmd
, ↵ Enter)
and type python -V
, ↵ Enter.
You should get a response back, something like Python 2.7.1
.
If you do not, you may not have Python installed. Fix this first.
Once you have Python, your batch file should look like
@echo off
python c:\somescript.py %*
pause
This will keep the command window open after the script finishes, so you can see any errors or messages. Once you are happy with it you can remove the 'pause' line and the command window will close automatically when finished.

- 4,739
- 1
- 33
- 46

- 55,315
- 8
- 84
- 99
-
8
-
3@the_prole for `@echo off` take a look at -> https://technet.microsoft.com/en-us/library/bb490897.aspx?f=255&MSPPError=-2147217396 and `%*` can take any number of arguments. – Akshay Aug 01 '16 at 04:31
-
2hello. this answer is wrong. how do you know python's path is added to environment variables? this will not work if it is not. – cagri Feb 05 '19 at 05:57
-
`python -i c:\somescript.py %*` opens the python's interactive mode in the same environment after running the script, so you can inspect global variables for debugging. – Nuno André Apr 12 '19 at 20:22
-
What if I don't want it to stay open even after running and just close instantly after it has started running? Something like what pythonw does? – Ieshaan Saxena Dec 30 '20 at 10:12
-
1
Here's how you can put both batch code and the python one in single file:
0<0# : ^
'''
@echo off
echo batch code
python "%~f0" %*
exit /b 0
'''
print("python code")
the '''
respectively starts and ends python multi line comments.
0<0# : ^
is more interesting - due to redirection priority in batch it will be interpreted like :0<0# ^
by the batch script which is a label which execution will be not displayed on the screen. The caret at the end will escape the new line and second line will be attached to the first line.For python it will be 0<0
statement and a start of inline comment.
The credit goes to siberia-man

- 55,367
- 18
- 148
- 187
-
1`%~f0` should be in quotation marks: `"%~f0"`, otherwise path with spaces or other special symbols will not be processed. To see this, call this file "My first script.bat" or places it in the folder "c:\My scripts". – Alexander Gelbukh Apr 06 '20 at 01:24
-
1Black autoformats to `0 < 0 # : ^` (giving _The system cannot find the file specified._) otherwise nice hack! – tejasvi88 Apr 03 '22 at 15:15
-
1@tejasvi88 are there any symbols after the `^`? It should be the last symbol on the line. I've just checked this and it is still working. – npocmaka Apr 03 '22 at 19:30
-
2@npocmaka It is working fine with `0<0# : ^` but not after formatting with Black. (which gives `0 < 0 # : ^`) – tejasvi88 Apr 04 '22 at 00:08
Just simply open a batch file that contains this two lines in the same folder of your python script:
somescript.py
pause

- 149
- 3
- 14
-
1
-
@RachitGupta somescript.py in above example is the filename of python script that you want to launch – Izzat Z. Sep 20 '16 at 02:04
-
@It'satrap Yes, see the answers by npocmaka, by Hans Ginzel, by Michael Villani and probably more, on this page. – Alexander Gelbukh Apr 06 '20 at 01:18
If you've added Python to your PATH then you can also simply run it like this.
python somescript.py

- 3,580
- 1
- 21
- 24
-
Yep that's an easy one, not so good if you have multiple Python installations (i.e 2.7 and 3.6 for example) also Python is almost always added to path so I don't think that's too much of a problem . – Xantium Sep 16 '17 at 17:56
-
2An option for that one is to make a copy in each of your python versions of the exe and rename to something unique. python2.7.exe, python3.6.exe etc. Then in the command line you can call it like python3.6 somescript.py – Chris Farr Sep 18 '17 at 13:13
-
2yes dreadfully obviously really but effective (why did I not think of that?). Also I think 36 be better than 3.6 and 27 for 2.7 the point might cause confusion. – Xantium Sep 18 '17 at 15:29
You can use python code directly in batch file, https://gist.github.com/jadient/9849314.
@echo off & python -x "%~f0" %* & goto :eof
import sys
print("Hello World!")
See explanation, Python command line -x option.

- 8,192
- 3
- 24
- 22
-
1How would you make the batch file pause once done? I can't add the pause anywhere. – Expliked Aug 23 '20 at 17:10
-
1You can do it in the python code as well, see [How do I make python wait for a pressed key?](https://stackoverflow.com/questions/983354/how-do-i-make-python-wait-for-a-pressed-key) – Hans Ginzel Sep 18 '20 at 08:29
--- xxx.bat ---
@echo off
set NAME1="Marc"
set NAME2="Travis"
py -u "CheckFile.py" %NAME1% %NAME2%
echo %ERRORLEVEL%
pause
--- yyy.py ---
import sys
import os
def names(f1,f2):
print (f1)
print (f2)
res= True
if f1 == "Travis":
res= False
return res
if __name__ == "__main__":
a = sys.argv[1]
b = sys.argv[2]
c = names(a, b)
if c:
sys.exit(1)
else:
sys.exit(0)

- 41
- 1
Similar to npocmaka's solution, if you are having more than one line of batch code in your batch file besides the python code, check this out: http://lallouslab.net/2017/06/12/batchography-embedding-python-scripts-in-your-batch-file-script/
@echo off
rem = """
echo some batch commands
echo another batch command
python -x "%~f0" %*
echo some more batch commands
goto :eof
"""
# Anything here is interpreted by Python
import platform
import sys
print("Hello world from Python %s!\n" % platform.python_version())
print("The passed arguments are: %s" % sys.argv[1:])
What this code does is it runs itself as a python file by putting all the batch code into a multiline string. The beginning of this string is in a variable called rem, to make the batch code read it as a comment. The first line containing @echo off
is ignored in the python code because of the -x
parameter.
it is important to mention that if you want to use \
in your batch code, for example in a file path, you'll have to use r"""..."""
to surround it to use it as a raw string without escape sequences.
@echo off
rem = r"""
...
"""

- 94
- 8
This is the syntax: "python.exe path""python script path"pause
"C:\Users\hp\AppData\Local\Programs\Python\Python37\python.exe" "D:\TS_V1\TS_V2.py"
pause
Basically what will be happening the screen will appear for seconds and then go off take care of these 2 things:
- While saving the file you give extension as bat file but save it as a txt file and not all files and Encoding ANSI
- If the program still doesn't run save the batch file and the python script in same folder and specify the path of this folder in Environment Variables.

- 403
- 3
- 7
If this is a BAT file in a different directory than the current directory, you may see an error like "python: can't open file 'somescript.py': [Errno 2] No such file or directory". This can be fixed by specifying an absolute path to the BAT file using %~dp0
(the drive letter and path of that batch file).
@echo off
python %~dp0\somescript.py %*
(This way you can ignore the c:\
or whatever, because perhaps you may want to move this script)

- 41,085
- 18
- 152
- 203
Use any text editor and save the following code as runit.bat
@echo off
title Execute Python [NarendraDwivedi.Org]
:main
echo.
set/p filename=File Name :
echo.
%filename%
goto main
Now place this file in the folder where python script is present. Run this file and enter python script's file name to run python program using batch file (cmd)
Reference : Narendra Dwivedi - How To Run Python Using Batch File

- 49
- 1
- 3
Create an empty file and name it "run.bat"
In my case i use "py" because it's more convenient, try:
C:
cd C:\Users\user\Downloads\python_script_path
py your_script.py

- 111
- 2
- 7
@echo off
call C:\Users\[user]\Anaconda3\condabin\conda activate base
"C:\Users\[user]\Anaconda3\python.exe" "C:\folder\[script].py"
ECHO OFF
set SCRIPT_DRIVE = %1
set SCRIPT_DIRECTORY = %2
%SCRIPT_DRIVE%
cd %SCRIPT_DRIVE%%SCRIPT_DIRECTORY%
python yourscript.py`

- 6,242
- 7
- 41
- 65

- 21
- 4
-
Please explain *what* your script does. It's a lot easier to read that way. – ifconfig May 08 '19 at 02:44
i did this and works: i have my project in D: and my batch file is in the desktop, if u have it in the same drive just ignore the first line and change de D directory in the second line
in the second line change the folder of the file, put your folder
in the third line change the name of the file
D:
cd D:\python_proyects\example_folder\
python example_file.py

- 59
- 4