Python script cannot execute in python window

2107
5
Jump to solution
04-07-2016 09:33 PM
Zheng_KiYip
New Contributor II

I have been using a python code to automate some mapping by loading the python script onto the GIS (10.2) python window and pressing Return to get it to start running. It has been working but for some reason the next time I loaded the code into the python window and pressed Return, instead of the code running, it went straight back to a command prompt. I cannot figure what has changed for this code to no longer get executed. Please help! Any suggestions would be very very helpful!

import arcpy
import numpy as np
mxd = arcpy.mapping.MapDocument("Current")
lstDataFrames=arcpy.mapping.ListDataFrames(mxd)
varLyrs = arcpy.mapping.ListLayers(mxd,'_Wt_norm*')
var_list = np.arange(0,16)
for ilyr in varLyrs:
    var_n = ilyr.name[:2].upper()
    
    for i in var_list:
        outpath = r"F://GIS_Data//SOM//Results_12Var4by4//Maps//" + var_n + "_N" + i + "_Norm.jpg"
        arcpy.ApplySymbologyFromLayer_management(ilyr, r"F://GIS_Data//Arcpy//SOM_Norm_Default.lyr")
        ilyr.symbology.valueField = "N" + str(i)
        ilyr.visible = True
        arcpy.mapping.ExportToJPEG(mxd, outpath)
        print("Node ", i)
        ilyr.visible = False
Tags (1)
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Esteemed Contributor

what would have been more useful is

print("Number of layers: {} varLyrs = {!s: }".format(len(varLyrs,  varLyrs))

to see if returns anything since if varLyrs is empty then your script worked perfectly and cycled through nothing because it was empty.

View solution in original post

0 Kudos
5 Replies
DanPatterson_Retired
MVP Esteemed Contributor

you will need some print statements.

There have been issues with people using Tab to do their spacing... don't

I would suggest loading it up in PythonWin, Pyscripter or whatever IDE is your choice and running it as a script.

In my experience, It is best to use an external IDE for anything other than a one-liner.  Others have had success with the built-in command prompt, an external one... no problem.

So you need some print statements thrown in to find out how far it got

Use a script rather than the command prompt for anything other than a one liner

0 Kudos
Zheng_KiYip
New Contributor II

Thanks for the suggestion, Dan! I've installed and loaded the code into PyScripter, changed all the tabs to spaces, and added some print statements. Then I ran it in Pyscripter, and found that the code only runs until it hits the for loop then it stops. Any idea why it doesn't go into the for loop? I've put a screenshot below.

geonet_scrnsht1.png

This is probably something obvious - does ArcMap need to be running when I run this script? I have been opening the mxd in arcmap before I run this code. So I'm wondering if that has anything to do with this...

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

what would have been more useful is

print("Number of layers: {} varLyrs = {!s: }".format(len(varLyrs,  varLyrs))

to see if returns anything since if varLyrs is empty then your script worked perfectly and cycled through nothing because it was empty.

0 Kudos
Zheng_KiYip
New Contributor II

That makes sense

I've replaced the print statement with the one you suggested but then I got a syntax error for the for loop line, which has never came up before...

geonet_scrnsht2.png

0 Kudos
Zheng_KiYip
New Contributor II

I've changed the print statement a bit just to print the length of the varLyrs list and found that it is 0 (see screenshot below). Then realized that I've accidentally deleted my wild card symbol in my ListLayers parameter. So after adding it back in, the script works fine now! Thanks so much, Dan, really appreciate your help!

0 Kudos