HOWTO · Batch

How to Run Python File in Batch Script

This tutorial covers how to run a Python file in Batch Script, providing step-by-step methods and code examples. Learn how to streamline your workflows by executing Python scripts directly from Batch files, passing arguments, and specifying Python paths. Perfect for developers and automation enthusiasts, this guide will enhance your scripting skills.

On this page

Running a Python script from a batch file is a Windows task: the examples below use .bat files and Command Prompt. You need an installed Python interpreter. You can call the python command when Windows can find it through PATH, or name a particular python.exe directly.

Check Python and prepare the files

Open Command Prompt and run python --version. A version number means that this command resolves to an interpreter; it does not prove that every Python installation is on PATH. If Windows reports that python is not recognized, use the interpreter’s full path below or add the intended Python installation to PATH. Keep the batch file and Python script in a known location so their paths are clear.

Run a Python script from a batch file

Save a batch file such as run_python.bat beside hello.py. The command @echo off hides the commands as they run. pause is optional: use it when you launch the batch file by double-clicking and want the window to stay open; omit it when another program or a terminal should receive control as soon as the script finishes.

A minimal batch invocation is @echo off followed by python hello.py. If the batch file may be started from another working directory, use a path based on the batch file’s own location: python "%~dp0hello.py". %~dp0 expands to the drive and directory containing the batch file, including a trailing backslash.

Choose a Python interpreter by its full path

Use an explicit executable path when python is unavailable through PATH or when the batch file must select a particular installation. In the batch file, write "C:\Program Files\Python312\python.exe" "%~dp0hello.py", replacing that example path with the location on your computer. Quote both paths so spaces are treated as part of the filename. Check that the executable and script paths actually exist; a quoted but incorrect path still fails.

Pass arguments to the Python script

In a batch file, %* forwards all arguments supplied to the batch file. For example, python "%~dp0greet.py" %* forwards them to greet.py. Call the batch file with quoted values that contain spaces, such as run_greet.bat "Ada Lovelace". The quotes keep the name together as one argument.

The Python script can read those values through sys.argv. Save this as greet.py, then call it through the batch command above. If no name is supplied, the script uses a default so it can also be run on its own.

import sys

name = sys.argv[1] if len(sys.argv) > 1 else "friend"
print(f"Hello, {name}!")
Hello, friend!

Handle paths and common execution problems

If Command Prompt says python is not recognized, check the command with python --version, then use the full path to python.exe or correct PATH. If Python starts but cannot open the script, verify the filename and location; %~dp0 is useful when the batch file should find a neighboring script regardless of the current directory. If a path includes spaces, put it in double quotes. If the script starts but reports a missing module, install that dependency in the same Python environment selected by the batch file. These commands use Windows batch syntax and do not apply unchanged to macOS or Linux shells.