Creating a BAT file for python script
c:\python27\python.exe c:\somescript.py %*
How to create a .bat file which execute python file
You will need to make 2 files, one for the windows command line (.bat
) another for the bash
script (.sh
). The reason being, after you start
the bash
console, it will work on different window and it has no idea what your .bat
contains. We shall call our scripts as boot.bat
and start.sh
respectively.
boot.bat
@echo off
title ML_autostart_API
start "C:\Program Files\Git\git-bash.exe" start.sh
Notice the start.sh
is added at the end of the start
command as parameter.
start.sh
# To activate python Environment
source E:/ML_APIs/Python_Environment/python3.8.10/Scripts/activate
# To navigate the project dir
cd E:/ML_APIs/API/Call_SessionV1
# To set the environment variables
source config/config.sh
# To run python application
python application.py
Note
Both scripts are in the same directory.
This answer assumes
python
is actually recognized ingit-bash
paths.Should this is not the case, you can just use the full path to the executable to call it.
A better alternative would be to just execute the bash script directly on start up (using that
start "C:\Program Files\Git\git-bash.exe" start.sh
), no mixing stuff.
How to create bat file for python file and allow another person to open it from their computer?
If you can count on them having installed Python globally (with admin privileges) and that the .bat
file will be in the same directory as the .py
file, you can use:
py.exe -3 "%~dp0\CompanyCommander.py"
py.exe
is the Windows launcher that is stored in a common location and can find installed copies of Python. -3
tells it to run the latest installed version of Python 3 available. %~dp0
is the drive & path containing the batch script, so if the Python file is in the same directory, it will be found.
Note that thanks to the Windows launcher, you may not need to use a .bat
file at all. If Python was installed with admin privileges and associated with the .py
extension, then all you need is a UNIX-style shebang line at the top:
#!/usr/bin/env python3
and to add a line at the very end of the script:
input("Press enter to exit...")
The Windows launcher recognizes shebang lines and handles them appropriately, and the input
function will provide the same basic functionality as a batch script's pause
.
bat file in Windows scheduler not running python script
The issue was the file wasn't able to save in the Network drive. The error said it couldn't find the file path. I used this code and solved the issue:
from pathlib import Path
ResultPath = Path(r'<network domain name>/Daily figures/')
ResultPath_str = str(ResultPath)
daily_figures = Presentation(ResultPath_str+'/'+Template/daily_figures_template.pptx')
Batch file with python script gives an error
A scheduled task runs with a working folder of (usually) C:\Windwos\System32
, which is write protected.
Give the full path: ... > "%userprofile%\xxx\log.txt 2>&1
or do a cd /d "%~dp0
before to switch to the folder where your script is located.
Related Topics
Pytz Localize VS Datetime Replace
Accessing Every 1St Element of Pandas Dataframe Column Containing Lists
Python Regular Expressions - How to Capture Multiple Groups from a Wildcard Expression
Count Consecutive Occurences of Values Varying in Length in a Numpy Array
Angles Between Two N-Dimensional Vectors in Python
Matplotlib: Overlay Plots with Different Scales
How to Enumerate an Object's Properties in Python
Generate a Random Derangement of a List
Why Not Generate the Secret Key Every Time Flask Starts
Play an Animated Gif in Python with Tkinter
How to Insert Newlines on Argparse Help Text
How to Write to a File, Using the Logging Python Module
Find Maximum Value of a Column and Return the Corresponding Row Values Using Pandas
What Is :: (Double Colon) in Python When Subscripting Sequences