Python not getting pasted correctly in ArcMap

943
6
06-16-2020 11:32 AM
AllenDailey1
Occasional Contributor III

Hello there,

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?

Thank you!

0 Kudos
6 Replies
JoeBorgione
MVP Emeritus

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?

That should just about do it....
Egge-Jan_Pollé
MVP Regular Contributor

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.

HTH,

Egge-Jan

More info:

Setting paths to data in Python—ArcPy Get Started | Documentation 

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.

AllenDailey1
Occasional Contributor III

Thanks for that tip!  hmmm.... my script has been working fine with the single backslashes, so your post makes me wonder why I haven't had problems with that!  

0 Kudos
AllenDailey1
Occasional Contributor III

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. 

--Allen

JoeBorgione
MVP Emeritus

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))
That should just about do it....
0 Kudos
AllenDailey1
Occasional Contributor III

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.

0 Kudos