Syntax error in python code exported from ModelBuilder in PRO for arcpy.EnvManager

722
3
Jump to solution
11-01-2022 03:01 PM
LoriMcCormack1
New Contributor III

I created a ModelBuilder routine and exported it to Python, so that I could add some custom code in the middle.

When I try to run the script, it stops with a syntax error in the final statement, pointing to the scratch workspace file geodatabase.

Any ideas on what I can fix?  I can't see the problem.  Thanks.

 

import arcpy

def ScheduledTaskMilepostsNeedsPythonPRO():  # Scheduled Task Mileposts Needs Python PRO

< my code >

if __name__ == '__main__':
    # Global Environment settings
    #  workspace=r"\\spokanecounty.org\publicworks\GIS\WorkShop\xArcGISProTasks\WorkRoadSegments.gdb"):
    with arcpy.EnvManager(outputCoordinateSystem="PROJCS["NAD_1983_HARN_StatePlane_Washington_North_FIPS_4601_Feet",GEOGCS["GCS_North_American_1983_HARN",DATUM["D_North_American_1983_HARN",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Lambert_Conformal_Conic"],PARAMETER["False_Easting",1640416.666666667],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",-120.8333333333333],PARAMETER["Standard_Parallel_1",47.5],PARAMETER["Standard_Parallel_2",48.73333333333333],PARAMETER["Latitude_Of_Origin",47.0],UNIT["Foot_US",0.3048006096012192]]", preserveGlobalIds=True, scratchWorkspace=r"\\mynetworklocation\xArcGISProTasks\WorkRoadSegments.gdb\",
                          workspace=r"\\mynetworklocation\xArcGISProTasks\WorkRoadSegments.gdb\"):
        ScheduledTaskMilepostsNeedsPythonPRO()

 

 

0 Kudos
2 Solutions

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

Check your paths, and simplify the spatial reference

import arcpy
sr = arcpy.SpatialReference(2926)
# sr.name 'NAD_1983_HARN_StatePlane_Washington_North_FIPS_4601_Feet'
with arcpy.EnvManager(
    outputCoordinateSystem=sr.name,
    preserveGlobalIds=True,
    scratchWorkspace=r"....gdb",
    workspace=r"....gdb"):

... sort of retired...

View solution in original post

0 Kudos
Luke_Pinner
MVP Regular Contributor

Three problems:

  1. scratchWorkspace=r"\\mynetworklocation\xArcGISProTasks\WorkRoadSegments.gdb\", 
  2. workspace=r"\\mynetworklocation\xArcGISProTasks\WorkRoadSegments.gdb\" 

Python raw strings r"\\some\string" can't end in a single backslash, i.e. r"\\some\string\" 

Why can't Python's raw string literals end with a single backslash?

    3. Your spatial reference is invalid as it is wrapped in double quotes and contains embedded double quotes.

Either do what @DanPatterson suggests, using the factory code (2926) to create a SpatialReference object (which I recommend), or wrap the spatial reference string in single quotes. 

And next time, include the full error message, please don't just say "a syntax error". You're removing valuable information that can help others help you.

View solution in original post

0 Kudos
3 Replies
DanPatterson
MVP Esteemed Contributor

Check your paths, and simplify the spatial reference

import arcpy
sr = arcpy.SpatialReference(2926)
# sr.name 'NAD_1983_HARN_StatePlane_Washington_North_FIPS_4601_Feet'
with arcpy.EnvManager(
    outputCoordinateSystem=sr.name,
    preserveGlobalIds=True,
    scratchWorkspace=r"....gdb",
    workspace=r"....gdb"):

... sort of retired...
0 Kudos
Luke_Pinner
MVP Regular Contributor

Three problems:

  1. scratchWorkspace=r"\\mynetworklocation\xArcGISProTasks\WorkRoadSegments.gdb\", 
  2. workspace=r"\\mynetworklocation\xArcGISProTasks\WorkRoadSegments.gdb\" 

Python raw strings r"\\some\string" can't end in a single backslash, i.e. r"\\some\string\" 

Why can't Python's raw string literals end with a single backslash?

    3. Your spatial reference is invalid as it is wrapped in double quotes and contains embedded double quotes.

Either do what @DanPatterson suggests, using the factory code (2926) to create a SpatialReference object (which I recommend), or wrap the spatial reference string in single quotes. 

And next time, include the full error message, please don't just say "a syntax error". You're removing valuable information that can help others help you.

0 Kudos
LoriMcCormack1
New Contributor III

Hey, Dan,

What an honor to get a reply from you.  I used your ArcIMS code years ago.  You're a legend.

I was able to get both options to work that Dan and Luke described above, with one correction.

I created SpatialReference object and using it in my EnvManager statement, and I got it to work once I changed outputCoordinateSystem=sr.name to outputCoordinateSystem=sr.

import arcpy
sr = arcpy.SpatialReference(2926)
# sr.name is 'NAD_1983_HARN_StatePlane_Washington_North_FIPS_4601_Feet'

def ScheduledTaskMilepostsNeedsPythonPRO():  # Scheduled Task Mileposts Needs Python PRO
<my code>
if __name__ == '__main__':
    # Global Environment settings
    with arcpy.EnvManager(outputCoordinateSystem=sr, preserveGlobalIds=True, scratchWorkspace=r"\\mynetworklocation\WorkRoadSegments.gdb",
                          workspace=r"\\mynetworklocation\WorkRoadSegments.gdb"):
        ScheduledTaskMilepostsNeedsPythonPRO()

I did what Luke mentioned by removing the double double quotes, and removing the ending \ in my two work space paths. 

import arcpy

def ScheduledTaskMilepostsNeedsPythonPRO():  # Scheduled Task Mileposts Needs Python PRO

<my code>

if __name__ == '__main__':
    # Global Environment settings
    #  workspace=r"\\spokanecounty.org\publicworks\GIS\WorkShop\xArcGISProTasks\WorkRoadSegments.gdb"):
    with arcpy.EnvManager(outputCoordinateSystem="PROJCS['NAD_1983_HARN_StatePlane_Washington_North_FIPS_4601_Feet',GEOGCS['GCS_North_American_1983_HARN',DATUM['D_North_American_1983_HARN',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Lambert_Conformal_Conic'],PARAMETER['False_Easting',1640416.666666667],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',-120.8333333333333],PARAMETER['Standard_Parallel_1',47.5],PARAMETER['Standard_Parallel_2',48.73333333333333],PARAMETER['Latitude_Of_Origin',47.0],UNIT['Foot_US',0.3048006096012192]]", preserveGlobalIds=True, scratchWorkspace=r"\\mynetworklocation\WorkRoadSegments.gdb",
                          workspace=r"\\mynetworklocation\WorkRoadSegments.gdb"):
        ScheduledTaskMilepostsNeedsPythonPRO()