Starting a Python Script from a .bat File
Why A .bat At All
Python scripts are awkward to launch on Windows. .py is associated with an editor for many installs, a double-click opens a console that closes the instant the script ends, and the script runs under whatever python happens to be on the PATH — which on a machine with several installations is not the one with your dependencies.
A small .bat fixes all four: it names the interpreter explicitly, runs the script, and keeps the window open long enough to read what happened.

The Launcher
@ECHO OFF
SET ThisScriptsDirectory=%~dp0
SET PyScriptPath=%ThisScriptsDirectory%main.py
"%ThisScriptsDirectory%venv\Scripts\python.exe" "%PyScriptPath%"
IF %ERRORLEVEL% NEQ 0 (
ECHO.
ECHO Script exited with code %ERRORLEVEL%
PAUSE
)
Save as run.bat beside the script. The venv\Scripts\python.exe path assumes a virtual environment in venv next to the batch file — adjust the folder name if yours differs, or point at the system interpreter if you are not using one.
Use a virtual environment. The whole point of naming the interpreter explicitly is that the one your script depends on is the one that runs. A global install is a machine-specific accident waiting to break.
What Each Line Does
%~dp0 — the batch file's own directory, with a trailing backslash. It is why the launcher still works if you move or rename the folder, and why it works from a network share.
@ECHO OFF — suppresses the command echo, so the window shows your script's output rather than the batch file's own lines.
IF %ERRORLEVEL% NEQ 0 — only pauses on failure. A PAUSE at the end of a successful run means a scheduled task hangs forever waiting for a keypress. Pausing only on error gives you the diagnostic window without breaking automation.
Quoted paths. %ThisScriptsDirectory% almost always contains a space (C:\Users\Luke K\...), and an unquoted path splits into two arguments. Every path in the file is quoted for that reason.
The Version Without A Virtual Environment
If you genuinely have no venv — a throwaway script, a clean machine — use the Python launcher rather than python:
@ECHO OFF
py -3 "%~dp0main.py"
IF %ERRORLEVEL% NEQ 0 PAUSE
py is the Windows Python Launcher, installed with Python from python.org. It reads the pyvenv.cfg if one is present, honours #! shebang lines, and picks the newest Python 3 by default. It is more reliable than python, which may be a Microsoft Store stub that opens the Store instead of running anything.
That stub is a genuinely common failure: python in a fresh Windows install opens an app store page, and the batch file appears to do nothing.
Capturing Output
For an unattended job, log instead of pause:
@ECHO OFF
SET LOG=%~dp0logs
un.log
IF NOT EXIST "%~dp0logs" MKDIR "%~dp0logs"
"%ThisScriptsDirectory%venv\Scripts\python.exe" "%~dp0main.py" >> "%LOG%" 2>&1
ECHO [%DATE% %TIME%] exit=%ERRORLEVEL% >> "%LOG%"
2>&1 merges stderr into the log, which is where Python writes tracebacks. Without it you capture the happy path and lose the reason it failed.
For long-running jobs, register the batch file as a scheduled task with -User 'SYSTEM' — and remember the task starts in C:\Windows\System32, so every path inside must be absolute or %~dp0-derived. A script that works on double-click and fails under the scheduler is almost always this.


Traps
Wrong interpreter version. A script needing 3.10 will fail obscurely under 3.8. Naming the venv's interpreter fixes it permanently.
Backslash doubling. Batch has no escape sequences, so %~dp0 already ends in \. Writing %~dp0\main.py produces a double backslash — usually tolerated, occasionally not. Append the filename with no extra separator.
Activated environments do not exist here. activate.bat is for interactive shells. Calling the venv's python.exe directly achieves the same thing and is deterministic.
Unicode and codepage. Batch files default to the system codepage. A script that prints box-drawing characters or emoji garbles on a machine with a different locale. Save the .bat as ANSI with ASCII-only content, and let Python handle encoding.
The current version of this file is at github.com/LukeKeam/python-start-script-bat.
Don't trust the download button. Trust the hash.
If this was useful, More hands-on tech guides at TechGeek. No pressure — read it whenever it's useful.