Can you run a BAT (Batch) file using ArcPy?

1173
9
01-04-2023 08:00 AM
CMcDonald
Occasional Contributor II

Hello,

is it possible to run a bat file using the version of Python (ArcPy) supplied with ArcGIS Pro 2.9 or ArcMap 10.8?

Thank you

0 Kudos
9 Replies
BlakeTerhune
MVP Regular Contributor

You wouldn't use arcpy, you would use the subprocess module built into Python.

https://stackoverflow.com/a/5469427

CMcDonald
Occasional Contributor II

Can regular Python from https://www.python.org  be installed also when my server already has a "Python27" folder setup as Arcmap 10.8 is installed on this machine already.

Thanks

0 Kudos
BlakeTerhune
MVP Regular Contributor

Installing ArcGIS (ArcMap or Pro) includes a full install of Python (2.x for ArcMap, 3.x for Pro). There's no need to install Python if you've already installed ArcGIS.

0 Kudos
Brian_Wilson
Occasional Contributor III

You can run any program at all that you have permission to run on the computer. Here is a quick example.

https://datatofish.com/batch-file-from-python/

 

0 Kudos
MichaelVolz
Esteemed Contributor

Are you trying to access python modules that are not installed by default during the installation of ArcMap or Pro?

Is this for the eventual goal of running a Windows Scheduled Task on a regular interval?

0 Kudos
CMcDonald
Occasional Contributor II

I would rather access python installed by ArcMap 10.8 if possible so that no additional software is required.

It won't be to run a Scheduled Task, the BAT I'm creating ultimately will trigger and FME Workspace to run but it has to be run on demand by the user as and when required.

0 Kudos
forestknutsen1
MVP Regular Contributor

Can you just have the user run the bat file from there desktops? Is the FME workspace and environment going to be on a server?

0 Kudos
CMcDonald
Occasional Contributor II

Yes I think you are right, I have been having a similar thought myself.  Ultimately I will need to create a Python script, an ESRI Tool, a Geoprocessing Task and then add that to a WAB just to allow a nicer interface for the users.

I could probably just put the BAT file on a shared folder on the server and add permissions so they can run the FME workspace on the server.

forestknutsen1
MVP Regular Contributor

Example of sub process use:

import argparse
import os
import subprocess


parser = argparse.ArgumentParser(description='initialize electric trace weights')
parser.add_argument('--env', help='target environment: dev, test , or prod', default='dev')
args = parser.parse_args()
env = args.env
database = {'dev': 'dev', 'test': 'test', 'prod': 'prod'}[env]
working_dir = os.path.dirname(os.path.realpath(__file__))
password = xxxx
trace_weight_exe = os.path.join(working_dir, 'ElectricTraceWeightInit.exe')
command = '"{exe}" {db} {pw}'.format(exe=trace_weight_exe, db=database, pw=password)
process = subprocess.Popen(command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
						   stderr=subprocess.PIPE)
exit_code = process.wait()
0 Kudos