Script works in ArcMap but not Pyscripter

401
9
Jump to solution
04-13-2020 09:24 AM
ChrisHolmes
Occasional Contributor III

Hello all,

I find myself in a situation where I have a script which goes through a mdx (or series of mdx's in a folder(s)) repairing broken links. It works fine from ArcMap as a script tool, but we are thinking it may be better to run it from outside ArcMap (as it can be rather time consuming) as a python script. I'm trying to run it using pyscripter and am getting an error early on. Here is the code:

import arcpy
import Engine2 as eng
import ConfigParser

config = ConfigParser.RawConfigParser()
configpath = r'X:\Development\ChrisH\BrokenLinks\Config\BrokenLinks.config'
config.read(configpath)

def main():
    mxdToCheck = config.get('data','mxdToReview')
    planningDataPath = config.get('data','planningDataPath')
    gdbSearchPath = config.get('data','gdbSearchPath')
    mxdFolder = config.get('data','mxdFolder')

    if len(mxdToCheck) > 0:
        brokenList = eng.GetBrokenLinks1(mxdToCheck)

def GetBrokenLinks1(mxdIn):
    #Get a list of broken layers in the passed in mxd
    mxd = arcpy.mapping.MapDocument(mxdIn)
    brknList = arcpy.mapping.ListBrokenDataSources(mxd)
    return brknList

The error occurs at line 21. Here is the error message:

Traceback (most recent call last):
  File "X:\Development\ChrisH\BrokenLinks\Scripts\links.py", line 185, in <module>
    if __name__ == '__main__': main()
  File "X:\Development\ChrisH\BrokenLinks\Scripts\links.py", line 41, in main
    brokenList = eng.GetBrokenLinks1(mxdToCheck)
  File "X:\Development\ChrisH\BrokenLinks\Scripts\Engine2.py", line 33, in GetBrokenLinks1
    brknList = arcpy.mapping.ListBrokenDataSources(mxd)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\utils.py", line 181, in fn_
    return fn(*args, **kw)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\mapping.py", line 1465, in ListBrokenDataSources
    result = mixins.MapDocumentMixin(map_document_or_layer).listBrokenDataSources()
  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\arcobjects\mixins.py", line 832, in listBrokenDataSources
    broken_sources = [l for l in self.layers if not l._arc_object.valid]
  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\arcobjects\mixins.py", line 683, in layers
    for frame in reversed(self.dataFrames):
  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\arcobjects\mixins.py", line 695, in dataFrames
    return map(convertArcObjectToPythonObject, self.pageLayout.dataFrames)
AttributeError: 'NoneType' object has no attribute 'dataFrames'

Thanks everyone

‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Tags (1)
0 Kudos
1 Solution

Accepted Solutions
ChrisHolmes
Occasional Contributor III

Thought I'd provide a final update as I found the issue which is raising this error. This script is running against approx 10000 mxd files on our network drive. I was able to figure out a file causing the error. It was a mxd which was created in June 2009 and last accessed in July 2009. When I try opening the mxd file with arcmap 10.5 I get this message, followed by arcmap opening a untitled map document:

For now, I've just added code to handle the error and move onto the next mxd file. I suspect there's numerous mxd on the network that are this old and haven't been touched in that long. Me thinks they probably shouldn't be there anymore, but that's not my decision. I'm just happy I've got things working.

Thanks!

View solution in original post

0 Kudos
9 Replies
DanPatterson_Retired
MVP Esteemed Contributor

do you want the main def to run when the script runs?

put these lines at the end of your script

if __name__ == "__main__":
    """optional location for parameters"""
    # optional controls here
    main()
ChrisHolmes
Occasional Contributor III

Sorry Dan, I forgot to include that at the bottom of the file is:

if __name__ == '__main__': main()
0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Sorry Chris, you will have to throw in some print statements to see what is going on and where it fails.

I also notice that this is arcmap 10.2?  (was pandas supported then?)

If your script is residing in a folder, it may need to know where the mxd is located, since scripts that work within the python console in map/Pro have it as a reference point

ChrisHolmes
Occasional Contributor III

Hi Dan,

The beginning of the script uses ConfigParser to get the name of the mxd to process. In this case the value in the config file is:

mxdToReview = \\coc\lupp\ops\Data\policy_plan_amalgamation\asp_data_gatherer_copycopy.mxd

I did not notice that the error message is referencing version 10.2, I am working in citrix and use Arcmap 10.5 in citrix, but the citrix server likely has 10.2 on it also. So when I open the .py files in pyscripter and run them somehow it's using 10.2? Not sure if that has anything to do with it. I will inquire with our IT.

It seems that the error occurs when it gets to: brknList = arcpy.mapping.ListBrokenDataSources(mxd)

I wouldn't imagine there's any difference between between 10.2 and 10.5 for that.

Thanks for pointing that out. I'm going to look into it some more. Always fun when it works in arcmap but not outside...

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

You mention too many things that cause issues.

Can you work with the data on a local machine with locally installed software? 

That would rule out the version issues and file location issues for a start.

ChrisHolmes
Occasional Contributor III

Ya, I can do that as a test. thanks

0 Kudos
ChrisHolmes
Occasional Contributor III

Thought I'd provide an update. I was able to get remote desktop access to a pc at work and then tested the script from a pc that has 10.5 installed on it and the script executed correctly. So it seems there's something going on the citrix server regarding 10.2 (which I'm told by our IT has been decommissioned). Anyway, they're looking into the 10.2 thing on the citrix server and it seems to me that issue is external to my script.

Thanks for the help.

DanPatterson_Retired
MVP Esteemed Contributor

Glad it worked out and you have narrowed down the source.

0 Kudos
ChrisHolmes
Occasional Contributor III

Thought I'd provide a final update as I found the issue which is raising this error. This script is running against approx 10000 mxd files on our network drive. I was able to figure out a file causing the error. It was a mxd which was created in June 2009 and last accessed in July 2009. When I try opening the mxd file with arcmap 10.5 I get this message, followed by arcmap opening a untitled map document:

For now, I've just added code to handle the error and move onto the next mxd file. I suspect there's numerous mxd on the network that are this old and haven't been touched in that long. Me thinks they probably shouldn't be there anymore, but that's not my decision. I'm just happy I've got things working.

Thanks!

0 Kudos