I built a model tool which is running perfectly. Because I need to add some more functionalities like filtering, which I can only do with the script, I export the model to script and set the filter which is working fine also. However, when I run the script, it failed and send me a message which i will attach. I guess maybe it is from my tools which is make query table or the SQL statements in the tool which consist inline substitution or something else might be wrong. I will post the script error message, the script and the model. Please kindly help ASAP.
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# single.py
# Created on: 2020-02-08 22:31:43.00000
# (generated by ArcGIS/ModelBuilder)
# Usage: single <Sitename> <Category> <Specie> <Start_Month> <End_Month> <Start_Year> <End_Year> <QueryTable> <QueryTable_Statistics>
# Description:
# ---------------------------------------------------------------------------
# Import arcpy module
import arcpy
arcpy.env.workspace = "C:\waterfowl\waterfowl\waterfowl.gdb"
arcpy.env.overwriteOutput = True
# Script arguments
Sitename = arcpy.GetParameterAsText(0)
if Sitename == '#' or not Sitename:
Sitename = "Abgrabung Malsch/Durmersheim" # provide a default value if unspecified
Category = arcpy.GetParameterAsText(1)
if Category == '#' or not Category:
Category = "Taucher" # provide a default value if unspecified
Specie = arcpy.GetParameterAsText(2)
if Specie == '#' or not Specie:
Specie = "Haubentaucher" # provide a default value if unspecified
Start_Month = arcpy.GetParameterAsText(3)
if Start_Month == '#' or not Start_Month:
Start_Month = "1" # provide a default value if unspecified
End_Month = arcpy.GetParameterAsText(4)
if End_Month == '#' or not End_Month:
End_Month = "3" # provide a default value if unspecified
Start_Year = arcpy.GetParameterAsText(5)
if Start_Year == '#' or not Start_Year:
Start_Year = "1966" # provide a default value if unspecified
End_Year = arcpy.GetParameterAsText(6)
if End_Year == '#' or not End_Year:
End_Year = "2007" # provide a default value if unspecified
QueryTable = arcpy.GetParameterAsText(7)
if QueryTable == '#' or not QueryTable:
QueryTable = "QueryTable" # provide a default value if unspecified
QueryTable_Statistics = arcpy.GetParameterAsText(8)
if QueryTable_Statistics == '#' or not QueryTable_Statistics:
QueryTable_Statistics = "C:\\waterfowl\\Scratch\\scratch.gdb\\QueryTable_Statistics1" # provide a default value if unspecified
# Local variables:
locationbirdinfo = "locationbirdinfo"
False = "true"
False__2_ = "true"
# Process: Make Query Table
arcpy.MakeQueryTable_management
("locationbirdinfo", QueryTable, "USE_KEY_FIELDS", "", "", "locationbirdinfo.locationinfo_wvz_zaehlgebiete_oag_karlsruhe_Sitename = '%Sitename%' And locationbirdinfo.birdinformation_KAT_Bezeichnung = '%Category%' And locationbirdinfo.birdinformation_ART_Bezeichnung = '%Specie%' And locationbirdinfo.birdinformation_Month_ >= %Start Month% And locationbirdinfo.birdinformation_Month_ <= %End Month% And locationbirdinfo.birdinformation_DAT_Datum >= %Start Year% And locationbirdinfo.birdinformation_DAT_Datum <= %End Year%")
# Process: Summary Statistics
arcpy.Statistics_analysis(QueryTable, QueryTable_Statistics, "locationinfo_wvz_zaehlgebiete_oag_karlsruhe_Sitename MAX;birdinformation_KAT_Bezeichnung MAX;birdinformation_ART_Bezeichnung MAX;birdinformation_Month_ MIN;birdinformation_Month_ MAX;birdinformation_DAT_Datum MIN;birdinformation_DAT_Datum MAX;birdinformation_DAT_Anzahl SUM", "")
Your parameters cannot contain spaces.
Did the output from my example look reasonable given my default values to test the string?
More generally, it looks like you were working with a table with joins or something since the table.field notation (eg.
locationbirdinfo.birdinformation_ART_Bezeichnung
may work well in a model but it may fail in a script.
More generally, is that the "exact" output from the model? or did you edit portions of the output?
It would be much better to break that one query into a series of building blocks then concatenate them at the end just prior to feeding it into the query.
Yes it is a joined table..it worked will in the model but fail in the Script. I will look at it all over again, remove the space also reset the output and let you know if it worked.
Hello, are you saying this could be a problem , and might not work in Script but only in model.
locationbirdinfo.birdinformation_ART_Bezeichnung
but It is important for me to use the joined table. Or are there any other ways to represent joined tables in python script.
It depends where you are running your script from. If you are doing it within an open session of arcmap or Pro, then it should work. If you are trying to run it disconnected, then you would be advised to make the joins permanent by saving to a new table so that you get field names that are permanent.
It is actually permanent because I exported it to file gdb. I think I am running within arcmap but I would love if I can run it within Pro 2.3, because that is where I built the model but because I cant export to script in Pro 2.3, I had to downgrade it to be able to open and export in arcmap.
Too bad
Export a model to Python—ArcGIS Pro | Documentation
You should have upgraded ArcGIS Pro to 2.5 instead
I cannot upgrade on my own ...only the school admin can do that. However, my script is working fine now without error but does not display on the table of contents on the map document.
You likely just need a return statement.
Myscript()
Return outputpath
Thanks but It did not work.
navigate to the folder where you saved the results and see it if was created in the first place. Not all operations add results to the map. ArcGIS Pro does by default