Error Pasting to Clipboard with Python in ArcMap 10.6

872
6
02-23-2018 09:02 AM
PhilipJutson
New Contributor

Error Pasting to Clipboard with Python in ArcMap 10.6

 

I have been testing an existing Python Add-In written for ArcMap 10.2 that throws an error in ArcMap 10.6. The error was 'module' object has no attribute 'argv'.  After checking the web it looked like code I used to past to the clipboard with Tkinker was the problem. To test I copied the following code from the Add-In and pasted into the Python Window and an error was generated on usage.

 

>>> import Tkinter

>>> def toClipboard(txt):

...     clip = Tkinter.Tk()

...     clip.withdraw()

...     clip.clipboard_clear()

...     clip.clipboard_append(txt)

...     clip.destroy()

...    

>>> toClipboard("Hello")

Runtime error

Traceback (most recent call last):

  File "<string>", line 1, in <module>

  File "<string>", line 2, in toClipboard

  File "C:\Python27\ArcGIS10.6\Lib\lib-tk\Tkinter.py", line 1814, in __init__

    baseName = os.path.basename(sys.argv[0])

AttributeError: 'module' object has no attribute 'argv'

>>> 

 

In ArcMap 10.2 this code runs without a problem. The wisdom of the web suggests that a similar problem happened in ArcMap 10.1 and could be to do with the way sys has been altered for ArcGIS.

 

Have I found a bug?

0 Kudos
6 Replies
DanPatterson_Retired
MVP Esteemed Contributor

sys.argv[0] is the script that the function is running from.

Create a script and try it.

import sys
script = sys.argv[0]
print(script)

# ---- result yours will show the full path, then the script name
Drive...:/...path to .../...script named... /untitled0.py

From the console, there is no script

import sys

print("sys.argv[0]: {!r:}".format(str(sys.argv[0])))

sys.argv[0]: ''
PhilipJutson
New Contributor

Thank you for taking the time to reply.  I have tried your last example in ArcMap 10.2.2 and 10.6. The results were:

ArcMap 10.6 Python Window

>>> import sys
>>> print("sys.argv[0]: {!r:}".format(str(sys.argv[0])))
Runtime error 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: 'module' object has no attribute 'argv'
>>> type(sys.argv[0])
Runtime error 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: 'module' object has no attribute 'argv'


ArcMap 10.2.2 Python Window

>>> import sys
>>> print("sys.argv[0]: {!r:}".format(str(sys.argv[0])))
sys.argv[0]: ''
>>> type(sys.argv[0])
<type 'str'>

sys.argv[0] is a null string in the 10.2.2 Python window and does not exist in 10.6 so there is a difference between ArcMap versions.  However the main question is how to get the code working in a Python Add-In.  It is just that I found the Python window handy for testing small bits of code.

How do I move forward?

My code does not use sys.argv. It is an ArcMap/Python supplied library (Tkinter) that uses sys.argv and I would

prefer to not have to alter underlying standard libraries.

Has there been a change in Python between 2.7.5 to 2.7.14 that changes how sys.argv is handled?

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

I notice the >>> 's in your examples which means you aren't running it from within a script but from the command prompt.

the sys.argv[0] trick only works when you load a script, and run it with 

import sys

as one of your imports, 

There is no point testing it from the command prompt.

PhilipJutson
New Contributor

Hi Dan

Why does it work in the ArcMap 10.2.2 Python Window?

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

You would have to backtrack through the changelog for python 2.7 to see when/where any changes occurred.  I suspect that it may have been an error or something.

I just wouldn't use sys.argv[0] at the command prompt in any event, besides in python 3.6 (.7)

sys.argv[0]
Out[4]: '' 

So it may have been returned to its original state somewhere in the intervening 10 years that python 3.x has been out.

As for trying to avoid importing the sys module,.... don't,  it will likely be imported somewhere along your code stream so I find it best to use it as one of my base imports in any script or module I am using to ensure that it has claimed namespace within the python environment.

0 Kudos
PhilipJutson
New Contributor

Hi Dan

 

I will make a note of your warning about Python 3. I’m not sure when ArcGIS will switch to Python 3 so I will be focusing on 2.7 as long as it is still being used.  Besides, having waited from 10.2 till 10.6 before upgrading, I cannot see the organisation I work for doing another upgrade for a while.

 

 

I have managed to come up with a practical solution this morning. In ArcMap 10.2.2 sys.argv[0] is an empty string so I tried the following:

 

>>> t= Tkinter.Tk(baseName='')

>>> del t

 

There were no errors so I altered my snippet of code to the following:

 

import Tkinter

def toClipboard(txt):

    clip = Tkinter.Tk(baseName='')

    clip.withdraw()

    clip.clipboard_clear()

    clip.clipboard_append(txt)

    clip.destroy()

 

 

This now copies to the clip board in the ArcMap 10.6 Python Window and behaves as I am used to In ArcMap 10.2. Maybe it isn’t the best way of doing it but it works and I don’t have a lot of time before 10.6 is rolled out in my organisation.

 

I had a quick look at the Python docs for Tkinter (https://docs.python.org/2/library/tkinter.html) and a quick scan had no warnings about sys.argv[0].

0 Kudos