PROBLEM

Batch files exit after the first Python command

Last Published: April 25, 2020

Description

When attempting to create a batch file to run a Python script multiple times, the batch file exits after only the first command is performed. The following is a code sample of a batch file that exhibits this behavior:

@echo Run my Python script
"%PROGRAMFILES%\ArcGIS\Pro\bin\Python\Scripts\propy" myscript.py
@echo Run my Python script AGAIN
"%PROGRAMFILES%\ArcGIS\Pro\bin\Python\Scripts\propy" myscript.py
@echo Finished			
In the sample above, the batch file only runs up to the second line that performs only the first myscript.py,  and ignores the second call.

Cause

In the code sample, the command, call, is not used. This results in the script being called as a standalone .py file to be executed instead of as a function in a batch script.

Solution or Workaround

To ensure the continuation of a Python batch script regardless of the account, and to avoid confusion about which version of Python is used in mixed environments, 64-bit or 32-bit, it is recommended to run the Python executable using the command prompt with the name of the Python file as an argument, as shown in the following example.
C:\Python27\ArcGIS10.5\python.exe "E:\My Scripts\DoConversion.py" c:\gisWork\gdb.mdb\counties 10
A common workflow is to have the task scheduler run a .bat file that in turn runs the script with arguments. The contents of the .bat is the single command line shown above. However, when using batch files to run multiple Python scripts, it is important to understand the syntax of the batch files. Adding call before each Python command ensures the batch file is running after the Python script exits, successfully running multiple Python scripts as intended. The following script is an example of a working script.
@echo Run my Python script
call "%PROGRAMFILES%\ArcGIS\Pro\bin\Python\Scripts\propy" myscript.py
@echo Run my Python script AGAIN
call "%PROGRAMFILES%\ArcGIS\Pro\bin\Python\Scripts\propy" myscript.py
@echo Finished

Article ID:000015230

Software:
  • ArcMap
  • ArcGIS Pro

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Related Information

Discover more on this topic