Select to view content in your preferred language

Automate Python Code

310
1
02-13-2026 10:21 AM
Labels (1)
HaileyTimmons
Regular Contributor

I am looking for a way to auto run python code when a project opens. I have it built in notebooks, but when I tried to make it into a script tool, it would never run in the active map. Does anyone has any ideas on how to get it to run code on start up?

0 Kudos
1 Reply
MErikReedAugusta
MVP Regular Contributor

I have a feeling the answer might depend on what code you're trying to have run on startup.

My initial gut reaction would be to say you actually need a wrapper around your project, instead of the reverse.

Your described order:

  1. Open Pro Project
  2. Code [auto-]fires off

 How I'd hypothetically accomplish that [in some instances]:

  1. Run batch file
  2. Batch file opens Pro
  3. Batch file calls python script

Where this gets tricky is that you mentioned the phrase "run in the active map", which implies to me that you're doing operations that require the arcpy.mp library, and you're using the token 'Current' in those function calls. (Or otherwise performing operations that are logically similar).

That's potentially quite a bit more complex, depending on what you're trying to do, because many of those functions presume that you have Pro open and are calling from inside Pro, if you're using that token.

 

So, I'd recommend reframing the question.  What are you ultimately trying to do?  Why did you arrive at "I need a script" and "the script needs to run automatically."

There's a solid chance we can get around the issue entirely by approaching the problem in a different way.

------------------------------
M Reed
"The pessimist may be right oftener than the optimist, but the optimist has more fun, and neither can stop the march of events anyhow." — Lazarus Long, in Time Enough for Love, by Robert A. Heinlein
0 Kudos