Creating a Bat File for Python Script

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 in git-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



Leave a reply



Submit