I have a ArcGIS Pro model which takes a polygon shapefile (Site_boundary_master) and spatially joins various layers to it, finally exporting the final output to an Excel spreadsheet:

my working model

All the joining layers are kept in various File Geodatabases.

I saved the model as 'AutomationModel' (but in the properties dialogue for it, it's called 'Model222'... wtf?) in a toolbox called 'Automation.tbx'.

Running the model from ModelBuilder works fine.

I then created a .py python file (from a blank .txt file) and wrote a script to run this model this way:

import arcpy
print ('Script started')
# import the toolbox
print ('Toolbox imported')

#import the model
print ('Model imported')
print ('Script finished')

Pasting this code into the ArcGIS Pro python console, it work fine. Again.

Script to run model works in python console

However, when I edit the .py file in IDLE (ArcGIS Pro) and then run the script, it doesn't work at all:

errors in IDLE

Can someone help me fix this?

Is it because I need to tell the script where all the input layers are stored?

If so, how would I write this?

I'm still a novice with python.

  • Please present errors as text rather than pictures so that they are available to future searches. As per the Tour there should be only one question per question. – PolyGeo Feb 8 '18 at 22:41

Your model has a lot of what looks to be LAYER input parameters. Meaning the input to the tools have come from the ToC inside Pro. When you run the model outside of Pro (ie. using python at command line), those layers don't exist. Thats what your error screen shot is saying. I cant find the input data

While you aren't really exporting your model to a script, you sort of are. In this help topic on exporting models it states:

If your model used layers or table views that weren't created within the original model, those layers or table views will have to be created in the script using tools such as Make Feature Layer and Make Table View.

Meaning you'll need to run tools to "make layers" PRIOR to running your model through your script. (Here's a link to the ArcGIS Pro syntax for making a feature layer.)

Alternatively, you can go back and edit your model and drag the featureclass into the tool inputs (ie a full path like c:\mydata.gdb\fc)

| improve this answer | |

The error you are getting for the most part look like "parameters are not valid" and "value does not exist" in the model. This makes me think that when you are accessing the file remotely in python it is not recognizing the correct environment. In short it does not know the proper full paths of the input parameters. Thus it is giving this error.

There are two things I would try right away

  1. Add a arcpy.env.workspace = "path/to/your.gdb" to the beginning of your script, this is essentially the same as setting the environment variables inside of arcpro (look here)

  2. You can export your model as a python script and debug it that way. Unfortunately this functionality was removed from Pro by ESRI (why, I do not know). So to do that you would have to open your model in ArcGIS Desktop. If you have access to Desktop there are instructions here.

    If you are at all python savvy this is a good way to do this. That being said executing spatial joins in python can sometimes be tricky for a beginner and may require you to create "field mappings".

| improve this answer | |
  • @simon-y thanks, I'll investigate. Am I right in thinking though that in order to spatial join, Pro would actually need to be open and running. If my script were to run overnight without Pro open, how the script know where the layers are in relation to each other spatially? – Theo F Feb 9 '18 at 9:58
  • No, it does not need to be open. The data is referenced to a specific location with defined geometry whether or not you are looking at it (think about the "shape" field in your feature classes and the coordinate systems you define in feature datasets). The computer can access the data anytime, you just have give it the correct set of commands to do exactly what you want. – Simon.y Feb 9 '18 at 16:31
  • thanks @simon-y It seems to fix my issue I just had to change the layer sources from the TOC to direct file paths. And the script als runs fine overnight. – Theo F Feb 14 '18 at 11:34

You basically need to read the help file! Look at this page on importing a toolbox. Look at the code sample at the end. You need to give your toolbox an alias.

You don't have to accept the silly model222 name. Look at the properties of your model, name will be model222 (which you can change to something sensible) and label is the human readable name for the model.

When you are learning python and arcpy get into the habit of reading the help file as geo-processing tools always have a code sample to get you going.

| improve this answer | |
  • I can't see anywhere on that help file which helps. I feel I've hit a wall here. My toolbox has an alias by default. It's the same name as the toolbox itself: 'Automation'. OK just curious about the Model222 name part. I don't really mind if it's called that. – Theo F Feb 8 '18 at 17:17
  • Using a good alias is best practice, but this doesn't solve the problem. The OP can successfully Import the toolbox. – KHibma Feb 8 '18 at 17:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.