invalid syntax:  MakeFeatureLayer_management

682
3
Jump to solution
05-17-2013 04:54 AM
DamonOsbourne
New Contributor II
Have both the streams and schools layers in my active arcmap session.  I am using arcmap's Python window to run this script in a python tutorial that finds schools within a specified distance from streams.  Here's the code, error is on line 9:

import arcpy
>>> arcpy.env.workspace = "c:/GIS/Python/data/TravisCounty"
>>> try:
...     streams = "Streams.shp"
...     streamBuffer = "StreamsBuffer.shp"
...     distance = "2640 Feet"
...     schools2mile = "Schools.shp"
...     arcpy.Buffer_analysis(streams,streamBuffer,distance,'FULL','ROUND','ALL')
...     arcpy.MakeFeatureLayer_management(schools2mile,'Schools2Mile_lyr')
...     arcpy.SelectLayerByLocation_management('Schools2Mile_lyr','intersect',streamBuffer)
...     except:
...         print 'Error'
...        
Parsing error SyntaxError: invalid syntax (line 9)
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
T__WayneWhitley
Frequent Contributor
I may be wrong but is the line 9 actually referring to your print statement?  I don't usually run code this way but I think you could confirm the suspicion by running without the try-except block...run the code statements directly, or if that isn't it, check your lyr references to make sure they're valid, although I pretty certain you'd get a different error intead of a 'parsing' error.


Hope that helps,
Wayne

NOTE:  Okay, I was curious to fiddle around with this some more and it seems you have an indention error with 'except' -- in my app, if typing in the commands manually, I have to backspace 4 spaces to 'align' the try/except....essentially this tells the interpreter that the 'except' block is beginning, that the 'try' block has ended.

I really didn't know this until I tried loading a script and noticed the left justification.  It may be more intuitive to write your script using IDLE and save it to a py file, then load it into the Python window by right-clicking in the window and navigating to where you saved your py file and selecting to open it...
I don't do it this way, but it may provide a more interactive experience for you, and it's nice to know I have that option.

Thanks,
Wayne

View solution in original post

0 Kudos
3 Replies
T__WayneWhitley
Frequent Contributor
I may be wrong but is the line 9 actually referring to your print statement?  I don't usually run code this way but I think you could confirm the suspicion by running without the try-except block...run the code statements directly, or if that isn't it, check your lyr references to make sure they're valid, although I pretty certain you'd get a different error intead of a 'parsing' error.


Hope that helps,
Wayne

NOTE:  Okay, I was curious to fiddle around with this some more and it seems you have an indention error with 'except' -- in my app, if typing in the commands manually, I have to backspace 4 spaces to 'align' the try/except....essentially this tells the interpreter that the 'except' block is beginning, that the 'try' block has ended.

I really didn't know this until I tried loading a script and noticed the left justification.  It may be more intuitive to write your script using IDLE and save it to a py file, then load it into the Python window by right-clicking in the window and navigating to where you saved your py file and selecting to open it...
I don't do it this way, but it may provide a more interactive experience for you, and it's nice to know I have that option.

Thanks,
Wayne
0 Kudos
DamonOsbourne
New Contributor II
Writing the script in IDLE first and then loading it was a great idea.  Made it much easier to troubleshoot.  Turned out the Streams layer wasn't valid for whatever reason, but the error message in the Arcpy window wasn't letting me know it.  Thanks for the idea, will load all future scripts through idle for just this reason.
0 Kudos
curtvprice
MVP Esteemed Contributor
Have both the streams and schools layers in my active arcmap session. I am using arcmap's Python window to run this script in a python tutorial that finds schools within a specified distance from streams. Here's the code, error is on line 9: 
import arcpy
>>> arcpy.env.workspace = "c:/GIS/Python/data/TravisCounty"
>>> try:
...     streams = "Streams.shp"
...     streamBuffer = "StreamsBuffer.shp"
...     distance = "2640 Feet"
...     schools2mile = "Schools.shp"
...     arcpy.Buffer_analysis(streams,streamBuffer,distance,'FULL','ROUND','ALL')
...     arcpy.MakeFeatureLayer_management(schools2mile,'Schools2Mile_lyr')
...     arcpy.SelectLayerByLocation_management('Schools2Mile_lyr','intersect',streamBuffer)
...     except:
...         print 'Error'
...         
Parsing error SyntaxError: invalid syntax (line 9)


Turned out the Streams layer wasn't valid for whatever reason, but the error message in the Arcpy window wasn't letting me know it.


As Wayne noted, your ArcMap python window version failed because you didn't indent it properly in, you guessed it, line 9.

Using an IDE like IDLE does indeed help with syntax issues like this; the python window is really best for running a single tool or trying out a few lines of code, not for entering/writing scripts. There are many useful IDE's to use -- IDLE is just the one that ships with all Python distributions -- it's the "vi" of Python IDE's - quick, minimal, always there.

Here's your indentation, fixed:

import arcpy
>>> arcpy.env.workspace = "c:/GIS/Python/data/TravisCounty"
>>> try:
...     streams = "Streams.shp"
...     streamBuffer = "StreamsBuffer.shp"
...     distance = "2640 Feet"
...     schools2mile = "Schools.shp"
...     arcpy.Buffer_analysis(streams,streamBuffer,distance,'FULL','ROUND','ALL')
...     arcpy.MakeFeatureLayer_management(schools2mile,'Schools2Mile_lyr')
...     arcpy.SelectLayerByLocation_management('Schools2Mile_lyr','intersect',streamBuffer)
... except:
...     print 'Error'
...         
0 Kudos