1

In one of my work, I need to go through about a few thousand mxd files, get some information from each MapDocument object (scale, printing paper size, map title, etc.), and write those information to a record (a csv file, for example).

My Python script run perfectly fine for the first 30 mxd files. When it reached the 31st mxd file, I got a Windows error saying "python.exe has stopped working. A problem caused the program to stop working correctly..." which gave me only 1 option to close the program. No error from the script itself. To be particular, the script failed at the line elms = arcpy.mapping.ListLayoutElements(...).

I run the script the 2nd time, this time I started from that 31st mxd file that I failed. The script run perfectly again for the next 97 mxd files. The same Windows error popped up, and again, Python crashed at the line elms = arcpy.mapping.ListLayoutElements(...).

I run the script the 3rd time, starting from the mxd file that the script failed to read. This time Python crashed after 49 mxd files, still at the line elms = arcpy.mapping.ListLayoutElements(...).

I repeated the above 3 run, and got exactly the same result, i.e. Python crashed after reading the exact number of mxd files, at exactly the same line.

I guess it's not the problem of the mxd file. It's probably arcpy itself or the way Windows and Python (or arcpy?) work together.

I then wrote a simple test script with just one for loop that repeatedly opens one of my mxd, and calls that line elms = arcpy.mapping.ListLayoutElements(...). Python crashed after 73-77 loops. I also tried with arcpy.mapping.ListDataFrames(mxd)[0] and mxd.pageSize, and they all made Python crash after 73-77 loops. Interestingly, some arcpy methods (e.g. mxd.dateSaved, mxd.description) did not cause the crash at all.

Anyone else has experienced this? I'm using Standard ArcGIS 10.5.

PART OF MY SCRIPT:

The last thing printed out before every Python crashes is the line print count, "mxd opened", so my best guess is that the next line elms = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "title") is what causing the crash.

def get_record(my_mxd):
    global count
    try:
        mxd = arcpy.mapping.MapDocument(my_mxd)
        count += 1
        print count, "mxd opened"
        elms = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "title")
        print "Element listed"
        if len(elms) == 0:
            fail_writer.writerow([my_mxd])
            return
        ...
    except Exception as e:
        print e
        fail_writer.writerow([my_mxd])

UPDATE 2

After several crashes and re-run the script starting from the crashed MXD, I finished reading ~900 MXDs and got another weird behavior. Instead of crashing, now Python gave me the error Object: CreateObject cannot open map document which was caught by try except, all the way to ~5000 MXDs.

But again, when I re-run the script, made it start from the first MXD that got this error, it run perfectly for the next ~100 MXDs, then throw the error Object: CreateObject cannot open map document all the way to ~5000 MXDs.

Looking forward to what I will get after this :)).

UPDATE 3

After ~2000 MXDs, the crash came back. This time I got some consistent crashes with several MXDs, i.e. re-run the script from the failed MXD kept crashing Python.

The fun part: some consistent MXDs crashed Python after pushing memory usage to 1GB, others crashed Python with just 170MB memory usage. At least Python struggled with those MXDs a few minutes before being knocked out. There's only one MXD that beat Python in 3s with just 170MB. All these MXDs can be opened in ArcMap without any problem.

  • Wow, that is odd. Try starting with a blank list of GoBackFiles, putting your function into a try: except: block and on exception add the MXD to the GoBackFiles list then iterate that on a 2nd attempt. Can we see some of your code that is generating the error? Though this sounds like a case for ArcGIS support. Are all the MXDs of the current version? Have you installed all the updates and patches? – Michael Stimson Mar 13 at 3:50
  • @MichaelStimson, I do have a try: except: in my script to print the current-processing mxd and any error encountered. Probably not all (or not at all) updates and patches are installed as it's a company license. Not sure if all MXDs are of current version (probably not) as they've been created by different users at different time with different update schedules. – Kevin Mar 13 at 4:11
  • Can you post a short snippet of code (not your whole script) that demonstrates the issue? This is a standard debugging technique - start small and add code until you can reproduce with a small test script. We can then try to reproduce. – user2856 Mar 13 at 5:05
  • No software should crash inside a script, but iteratively opening thousands of map documents is a worst case scenario in terms of tempting fate. Far better to open and process at most 40 files, marking each file completed with a parallel .mok file, and have the script ignore processed files. – Vince Mar 13 at 10:17
  • Don't know if this will be a red herring but try adding a del mxd,elms within your loop, may be that might release the mysterious use of memory? – Hornbydd Mar 13 at 10:47
0

In my opinion there are some many reasons cause to get this error message, I'd same problem, I recommend, if your're running your code via Jetbrains pycharm IDE or any other unofficial IDE for python such as (Spyder IDE, Visual Studio,etc), run your code by Python IDLE, that officially belong to python, moreover try put your code in try and except ,or if these solution can't fix your error, restart your PC and don't run ArcMap or ArcCatalog, run your code in IDLE again,But if these can't fix your problem, use these tools you can find them in path such below these paths:

MXD Doctor "C:\Program Files (x86)\ArcGIS\Desktop10.3\Tools\MXDDoctor.exe" or ArcGIS Document Defragmenter "C:\Program Files (x86)\ArcGIS\Desktop10.3\Tools\DocDefragmenter.exe"

remmeber: you must be sure all mxd files are in newer version not older than your version your're working now.

  • 1
    I'm calling my script from command prompt, not from any IDE. I also have try-except in my script to catch any unexpected error. I'll try the restart and update the result then. – Kevin Mar 13 at 5:37
  • please read this community.esri.com/docs/DOC-7545-fixing-arcmap-crashes and try fix mxd files you think they due to stop and crash python – Predator X Mar 13 at 5:45
  • don't forget check all mxd files are in new version!! before do everything @Kevin – Predator X Mar 13 at 5:54
  • As I said in the question, the script run just fine if I make it start from the failed mxd. So I think it's safe to say that it's not the mxd causing the crash. – Kevin Mar 13 at 5:58
  • ok if you think problem back to your code not mxd files, upload your code may be I and other guys can find out what is problem exactly, without code I can't help you more than things I wrote above, at least not yet – Predator X Mar 13 at 6:05
0

Found a thread that is pretty much what happen to me:

https://community.esri.com/thread/106502

Not really an "answer" as the problem was described as "memory leaks" issue in Python 2.7, and no solution was found.

Migrating to ArcGIS Pro (running on Python 3.x) would clear the issue, if you can manage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.