3

I am new to Python and coding generally, and I cannot figure out how to set up a debugger that will work with Arcpy. I am trying to use PyCharm, but it always gets caught up with the fact that it can't actually open ArcMap. It gives me this error when I run in the IDE:

RuntimeError: Object: CreateObject cannot open map document.

I run the scripts from a Toolbox and they don't run correctly. All I know how to do is comment out some lines and narrow the problem down by re-running. There's got to be a better way. Right?

  • 3
    Does 'import arcpy' work for you in PyCharm? – Nir Aug 27 '15 at 21:02
  • You don't need PyCharm. ArcPy is a Python module like others and there are many solutions for debugging scripts (look at Debugging Python Like a Boss for example). pdb is a standard module (in all Python distributions) – gene Aug 27 '15 at 21:13
  • It does. All PyCharm is to me right now is a text editor that (usually) autocompletes Arcpy. – agarfield Aug 27 '15 at 21:14
  • 1
    There's nothing wrong with wanting autocomplete (intelisense) as it can be a real timesaver especially as python is case sensitive. I use PyWin (on Win7) and that will autocomplete after you've imported arcpy in the interactive window. As @Nir said, can you import arcpy in your IDE/Editor? If you can't open a map document then there's possibly something wrong with your syntax... note that opening a document from python is not the same as launching ArcMap (you wont see the document open) - are you trying to write geoprocessing scripts or python plugins? (yes, there's a huge difference) – Michael Stimson Aug 27 '15 at 21:51
  • Following up on Michael, When you type "import arcpy" and execute, do you get an error? – Nir Aug 27 '15 at 22:32
2

If your scripts are failing at lines where you're using the arcpy package, you can use try & except statements. In the try statement you should put your arcpy methods, and in the except you can write out arcpy.GetMessages() to a log file.

arcpy.getMessages() writes out the messages from the last geoprocessing event attempted. It may help you identify where your script is going wrong.

  • It you really want to debug a script, it is not the Python proper way, use a debugger module as pdb – gene Aug 27 '15 at 21:22
0

paste your python script into the python window in arcmap and execute it from there. the error massage will tell you what line the error occurs on.

0

For large scripts I use Eclipse. Eclipse is a very powerful, respected and free IDE that can be used for a number of languages. Eclipse can also play nicely with source control such as Subversion etc, which is handy for larger projects where a few developers are working together. For Python development you need the PyDev plugin. Setting up Eclipse for development with ArcPy is fairly straightforward. There are many tutorials out there and a quick search yields this one.

0

I tend to put major bits of py in try statements. The "AddMessage()" and "AddError()" and "except Exception as e:" are all rather handy at reporting different things.

        desc = arcpy.Describe(raster)
        SR = desc.spatialReference
        try:
            arcpy.AddMessage("         - Band Count:        %d" % desc.bandCount)
            txtRasterFile.write (desc.bandCount+",")
        except:
            arcpy.AddMessage("         - Does Not Support Bands")
            txtRasterFile.write ("Unsupported,")
        try:
            arcpy.AddMessage("         - Compression Type:  %s" % desc.compressionType)
            txtRasterFile.write (desc.compressionType+",")
        except:
            arcpy.AddMessage("         - Does Not Support Compression")
            txtRasterFile.write ("Unsupported,")
        try:
            arcpy.AddMessage("         - Raster Format:     "+desc.format)
            txtRasterFile.write (desc.format+",")

        except:
            arcpy.AddError("          - Raster Format: Error") #Error Message
            txtRasterFile.write ("Error,")
        try:
            arcpy.someprocess()
        except Exception as e:
            arcpy.AddError("       - Processes Faled: "+e.message)

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.