- 1 year ago
How to make python 3 scripts execute from .bat file on Windows 10?
To execute Python 3 scripts from a .bat file on Windows 10, you can follow these steps:
Create a Python Script:
First, create a Python script that you want to execute. Save it with a .py extension. For example, you can create a simple script namedexample.py
with the following content:pythonprint("Hello from Python script!")
input("Press Enter to exit...")
Create a Batch Script (.bat):
Open a text editor and create a new batch file (a file with a .bat extension). For example, create a file namedrun_python_script.bat
and add the following content:batch@echo off
REM Change "python" to the appropriate Python executable name if necessary
REM (e.g., python3, python3.8, etc.)
REM Set the path to your Python executable (if not added to PATH)
set "python_exe=python"
REM Set the path to your Python script
set "python_script=example.py"
REM Execute the Python script
%python_exe% %python_script%
REM Pause to keep the console window open after execution (optional)
pause
In the batch script, you need to specify the correct path to your Python executable (
python_exe
) and the path to your Python script (python_script
). Make sure to use the appropriate Python executable name, especially if you have multiple Python versions installed.Save the Batch Script:
Save the .bat file in the same directory as your Python script.Run the Batch Script:
Double-click therun_python_script.bat
file to execute the Python script. The batch script will call the Python interpreter specified by%python_exe%
and run your Python script (%python_script%
).
The batch script will open a command prompt window, execute the Python script, and display the output in the console window. It will also pause the console window after execution, which is optional and can be removed if you don't want to keep the window open.
By using this approach, you can easily execute Python 3 scripts from a batch file on Windows 10 and perform various tasks that involve running Python scripts with specific configurations or parameters.