I've been working on creating some Python scripts, and I've been testing out parts of them by copying and pasting the code from PyCharm into the Python window in ArcMap. That has worked perfectly for many days, until today. Today, when I've tried copying and pasting this script (which is for undoing some of what my real script does, so that I can re-test it), it gets pasted in an unusual font. I HAVE RUN THIS SCRIPT SUCCESSFULLY BEFORE. But today, it does not seem to be recognized as code. It does not automatically get the dots on the left-hand side of the lines of code that it normally gets. And the code does not run, as you can see in the error message at the bottom.
Can anyone help me get back to the correct style pasting? Why is this happening?
Not sure what might be going on in ArcMap, and I don't use pycharm, but can't you run snippets of code from a pycharm console? That's what I like to do with Spyder. Are you using the same .mxd? Maybe try creating a new one?
Hi Allen Dailey,
Hmmm, I am not sure, but it doesn't looks like there is any invalid syntax in line 1... "import arcpy", what can be wrong with that?!
I have always been taught, when looking for the error, to also have a look at the line before and/or the line after the one indicated by the parser.
So, let's have a look at line 2, the line where you set the path to your data: you are using backslashes here. And maybe that's what is hurting...
You should replace these backslashes either by forward slashes, or by double backslashes.
Programming languages, such as Python, treat a backslash (\) as an escape character. For instance, \n represents a line feed, and \t represents a tab. When specifying a path, a forward slash ( /) can be used in place of a backslash. Two backslashes can be used instead of one to avoid a syntax error. A string literal can also be used by placing the letter r before a string containing a backslash so it is interpreted correctly.
Thanks for your suggestions, Joe and Egge-Jan!
I quit and then reopened ArcMap and tried again, and this time the code was pasted normally and it worked. I'd still be quite interested to hear if anyone has an answer to my question, just out of curiosity.
But I guess the moral of the story is: if you have a problem, quit the application and then try again.
I can never remember when to escape a slash, so I like to copy and paste the path out of a catalog pane in pro and use the raw text indicator (r). For example to get an output of field names and types in a tuple format:
table = r'C:\GIS\Paradox\BUSS\Business.gdb\BusinessLicense' for f in arcoy.ListFields(table): print((f.name, f.type))
Oh, huh! I've learned from experience that it's necessary to have the "r" in front of the path, but I didn't know what it actually meant or did, so that's a big help to know that it helps prevent unintended escapes due to backslashes! That makes sense.