Select to view content in your preferred language

Python Crash

2501
5
Jump to solution
09-27-2012 12:31 PM
by Anonymous User
Not applicable
Original User: Caleb1987

Since county residents come into my office on a daily basis and ask for maps of their property, I decided to make a script that will allow me to print a map for them programmatically.  I wrote a script to do this and it works perfectly (I can choose to search by Parcel ID or address and it will zoom to that area and print, then a loop will allow me to keep printing maps as many times as I want).  However, the first version was pretty ugly since it was the testing version (MASTER_SEARCH_ugly.py, hence the name) and I wanted to clean it up a bit to make it run more efficiently. 

Since there were 3 functions being used I had to use global variables for the clean version so that they could be accessed by the function.  For some reason in the clean version (MASTER_SEARCH_clean.py), python crashes every single time and I get the "Python.exe has encountered a serious error and will have to close..etc" message.  The script will allow me to run the process ONLY once, and if I tell it I want to print another map it will let me do so. Then, as it is exporting to PDF it crashes every single time (only on the second map).  I have tried different variations such as making a map from searching by parcel, then address, vice versa, then by 2 different parcels, and by 2 different addresses.  Same result every time, it will work only once, then while working on the second map it crashes.  If I decide to print only 1 map, it will let me exit the program cleanly.

There are no errors in the script and I get no error messages except for the windows message.  I have no idea why this is happening.  I suppose is not a huge deal since the "ugly" version works perfectly, but I was hoping to make this script a little more dynamic with less hard-coding.  My guess is that the problem is coming from calling the global variables for the functions.  I just don't understand why it is crashing python.

I have attached both scripts as well as some screenshots that may help explain what is going on.  It just doesn't make sense to me why it crashes in the clean version.  I talked to an analyst at Esri earlier, I think they are still testing since I have not heard back from them yet.  I was informed to uninstall my X Tools Pro as that can cause problems with arcpy and it is still crashing.  I am running ArcEditor 10.0 SP 5 on Windows XP.

If anyone may know why this is happening please let me know! Thanks!
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable
Original User: mahunter243

After a 2nd look, I see you are deleting the reference to mxd in the printpdf function.  This is not good, take that out.  That will probably fix your problem, but I'd recommend loosing the globals altogether.  I've learned from hard experience that they come back to bite you often, just like this.  Outer-scope constants are fine, but any name that's going to have the object it points to change, or is pointing to a mutable object, is better passed to functions as an argument.  If you had done that, then your del statement would be no problem.

Anyway, take out the del statement, and you should be good to go.

Mike

View solution in original post

0 Kudos
5 Replies
by Anonymous User
Not applicable
Original User: mahunter243

Just a hunch, but I'd try taking out the line:

df.extent = parcels.getSelectedExtent(True)


Since you do a zoomtoselectedfeatures just above it, it is unnecessary unless there are selected features in other layers in the saved mxd.  And if there are, I'd unselect them and save it that way.  My hunch is that you've run into a bug (NIM082523) that causes a crash when you get extents from layers in map docs that have been created or addressed with GP tools.  I may be wrong, but it looks suspiciously like the issue in this post:

http://forums.arcgis.com/threads/61570-Layer.getExent()-Crashes-Arcmap

Maybe not, but it's worth a try.  I don't see anything else glaringly wrong.

good luck,
Mike
0 Kudos
by Anonymous User
Not applicable
Just a hunch, but I'd try taking out the line:

df.extent = parcels.getSelectedExtent(True)


Since you do a zoomtoselectedfeatures just above it, it is unnecessary unless there are selected features in other layers in the saved mxd.  And if there are, I'd unselect them and save it that way.  My hunch is that you've run into a bug (NIM082523) that causes a crash when you get extents from layers in map docs that have been created or addressed with GP tools.  I may be wrong, but it looks suspiciously like the issue in this post:

http://forums.arcgis.com/threads/61570-Layer.getExent()-Crashes-Arcmap

Maybe not, but it's worth a try.  I don't see anything else glaringly wrong.

good luck,
Mike


Mike,

Thanks for the reply.  It is definitely worth a shot, I'll try it tomorrow.  However, the other script uses the getExtent() as well and works perfectly, while this one crashes.  I will remove the getExtent().  I have not played with the mapping module too much except for this script, so I guess I thought I needed to get the extent first in order to change the scale?  After reading the thread you mentioned, this definitely seems like it could be the culprit, although I am not using 10.1.  I'll let you know if it works! Thanks again!

Caleb
0 Kudos
by Anonymous User
Not applicable
Original User: Caleb1987

After removing the df.getSelectedExtent(), I am still having the same problem with it crashing when it is ran a second time.
0 Kudos
by Anonymous User
Not applicable
Original User: mahunter243

After a 2nd look, I see you are deleting the reference to mxd in the printpdf function.  This is not good, take that out.  That will probably fix your problem, but I'd recommend loosing the globals altogether.  I've learned from hard experience that they come back to bite you often, just like this.  Outer-scope constants are fine, but any name that's going to have the object it points to change, or is pointing to a mutable object, is better passed to functions as an argument.  If you had done that, then your del statement would be no problem.

Anyway, take out the del statement, and you should be good to go.

Mike
0 Kudos
by Anonymous User
Not applicable
After a 2nd look, I see you are deleting the reference to mxd in the printpdf function.  This is not good, take that out.  That will probably fix your problem, but I'd recommend loosing the globals altogether.  They come back to bite you often, just like this.  Outer-scope constants are fine, but any name that's going to have the object it points to change, or is pointing to a mutable object, is better passed to functions as an argument.  If you had done that, then your del statement would be no problem.

Anyway, take out the del statement, and you should be good to go.

Mike


Mike,

You sir, are a genius!!! I did not even notice I had the del mxd function in the printpdf() function! I was supposed to take that out when I made it into its own function in the clean version, but clearly I forgot.  Now it definitely makes a lot more sense as to why it wasn't working. 

as for global, I used that because the functions were not able to access those variables, and as I am a novice python scripter, I had no other idea as to how to have those functions read the variables at the top of my script.  It is working perfectly now! Thanks again for helping me out!
0 Kudos