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?

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Aug 27, 2015 at 20:59
6
  • 3
    Does 'import arcpy' work for you in PyCharm? Commented Aug 27, 2015 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) Commented Aug 27, 2015 at 21:13
  • It does. All PyCharm is to me right now is a text editor that (usually) autocompletes Arcpy. Commented Aug 27, 2015 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) Commented Aug 27, 2015 at 21:51
  • Following up on Michael, When you type "import arcpy" and execute, do you get an error? Commented Aug 27, 2015 at 22:32

4 Answers 4

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.

answered Aug 27, 2015 at 21:15
1
  • It you really want to debug a script, it is not the Python proper way, use a debugger module as pdb Commented Aug 27, 2015 at 21:22
-1

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.

answered Aug 28, 2015 at 4:17
-1

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.

answered Aug 28, 2015 at 6:28
-1

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)
answered Aug 28, 2015 at 8:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.