Receiving a "'module' object has no attribute 'argv'" in Tkinter

I'm trying to retrieve a variable value in one class (cboClipLayer) that calls a second class (class clippingEntry) which implements Tkinter.  However, whenever I make the call, Iargv-problem.txt receive the message "'module' object has no attribute 'argv'".  The exact message is provided in the attached file.

The call from cboClipLayer simply says:
clippingValue = clippingEntry()

and the start of the clippingEntry code is as follows:

from Tkinter import *

class clippingEntry(object):

    def __init__(self):
        window = Tk()

I don't know what argument this thing is looking for, and I would appreciate any insight that you can provide.  Please note that I get the same result whether or not I import the sys library. Thank you.
cshore12Asked:
Who is Participating?
 
peprConnect With a Mentor Commented:
For the sys -- yes, the argv is defined inside the standard sys module, while the argx is not. It means that your project knows some sys module, but that module does not contain the argv. The sys module is one of the built-in modules. It means there is no sys.py in the standard distribution. You can try:
import sys
print sys.builtin_module_names

Open in new window

and you get
('__builtin__', '__main__', '_ast', '_bisect', '_codecs', '_codecs_cn', '_codecs
_hk', '_codecs_iso2022', '_codecs_jp', '_codecs_kr', '_codecs_tw', '_collections
', '_csv', '_functools', '_heapq', '_hotshot', '_io', '_json', '_locale', '_lspr
of', '_md5', '_multibytecodec', '_random', '_sha', '_sha256', '_sha512', '_sre',
 '_struct', '_subprocess', '_symtable', '_warnings', '_weakref', '_winreg', 'arr
ay', 'audioop', 'binascii', 'cPickle', 'cStringIO', 'cmath', 'datetime', 'errno'
, 'exceptions', 'future_builtins', 'gc', 'imageop', 'imp', 'itertools', 'marshal
', 'math', 'mmap', 'msvcrt', 'nt', 'operator', 'parser', 'signal', 'strop', 'sys
', 'thread', 'time', 'xxsubtype', 'zipimport', 'zlib')

Open in new window

Because of that my suggestion to search for (another) sys.py was a blind move. I am not sure, and I did not try if the sys can be obscured by the external sys.py. (Tried later and it seems it cannot be masked.) However, there are some ways in Python to hide say argv if you really want. You can simply delete the attribute like this:
import sys
print dir(sys)
del sys.argv
print dir(sys)
print sys.argv

Open in new window

And you will observe (shortened)
c:\tmp\___python\cshore12\Q_28241147>d.py
['__displayhook__', '__doc__', ...,  'api_version', 'argv', 'builtin_module_names',
'byteorder', ..., 'winver']
['__displayhook__', '__doc__', ..., 'api_version', 'builtin_module_names', 'byteorder', ...,'winver']
Traceback (most recent call last):
  File "C:\tmp\___python\cshore12\Q_28241147\d.py", line 5, in <module>
    print sys.argv
AttributeError: 'module' object has no attribute 'argv'

Open in new window

This is the reason I have suggested to search for the sys usage in your (or in ArcGIS) sources.

For the service/test1 -- it works for me. The output lines in the above comment were produced by your program.
0
 
peprCommented:
The problem is that the mentioned Tkinter is part of the ArcGIS10.1, as the error message reports (C:\Python27\ArcGIS10.1\Lib\lib-tk\Tkinter.py). I do not know the reason for doing that. Anyway, the official Tkinter.py can also be found in c:\Python27\Lib\lib-tk\Tkinter.py, and the line 1680 contains the same code. With the lines around it says:
            import sys, os
            baseName = os.path.basename(sys.argv[0])
            baseName, ext = os.path.splitext(baseName)

Open in new window

This should work and it should return the name of the script. Then the extension is removed and other extensions are added when checking further.

This was created as fairly usual code that uses only standard modules. You can try the three lines in a separate script (after fixing the indentation); add the print command.

Anyway, ArcGIS plays the role here -- the Tkinter.py may be somehow modified. I cannot help without having more info.
0
 
cshore12Author Commented:
Thanks, pepr.  The ArcGIS version indeed does contain these 3 lines in the Tk() constructor.  When I ran them separately, they correctly identified the name of the script.

I'm not sure what additional information would be useful here.  I could send you the entire Tkinter.py script, but I think then you would be looking for a needle in a haystack.  Let me know if there's something more specific you need to look at.

In any event, I'm also going to elevate this issue with ESRI.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
cshore12Author Commented:
I think the problem may be related to the fact that the Tkinter-implemented class is in the same module as everything else.  So now I've separated out the former into its own module, and I'm trying to import into the main module.  However, when I do that, my application errors out.

I've created two very simple modules called service.py and test1.py (see attached).  When I try to import test1 into the service module, I get the message that the (test1) module isn't recognized.  I've gone to about a dozen sites indicating how to do this, and, this being Python, none of them work.  Could you please take a look at the 2 modules and tell me where I'm going wrong?
service
test1
0
 
peprCommented:
It is likely that the Python interpreter says the truth. It says "AttributeError: 'module' object has no attribute 'argv'". Let's take it seriously. It means that the used sys module is not the standard sys module. Try the following code:
import sys
print sys.argv     # the argv exists in the sys module
print sys.argx     # the argx does not exist in the sys module

Open in new window

This is simplification of the situation (sys only, no indexing). It prints the following on my console:
c:\tmp\_Python\cshore12\Q_28241147>b.py
['C:\\tmp\\_Python\\cshore12\\Q_28241147\\b.py']
Traceback (most recent call last):
  File "C:\tmp\_Python\cshore12\Q_28241147\b.py", line 3, in <module>
    print sys.argx     # the argx does not exist in the sys module
AttributeError: 'module' object has no attribute 'argx'

Open in new window

Notice that the error message has exactly the same form. Try to find whether some sys.py file is not the part of your solution. Search also for any usage of the sys identifier in the sources. (But this is just a blind guess.)
0
 
peprCommented:
If you fix the service.py like:
#!/usr/bin/python

import sys
import test1

class A:
    def __init__(self):
        self.enabled = True

    def service_func(self):
        print 'service func'

if __name__ == '__main__':
    # service.py executed as script
    # do something
    a = A()
    a.service_func()
    c = test1.B()
    c.some_func()

Open in new window

You should observe:
c:\tmp\_Python\cshore12\Q_28241147>py service.py
service func
foo
in test 1, unproductive

Open in new window

The service_func definition missed the self argument, and the instance of that class must be created first. If you mean to use service_func() only as a normal function inside the module, then you must undent it (one indentation level to the left so that is not the part of the A class definition:
#!/usr/bin/python

import sys
import test1

class A:
    def __init__(self):
        self.enabled = True

def service_func():
    print 'service func'

if __name__ == '__main__':
    # service.py executed as script
    # do something
    service_func()
    c = test1.B()
    c.some_func()

Open in new window

It is also unusual to use parentheses in the class head definition if you do not inherit from another class.
0
 
cshore12Author Commented:
I ran your code and got the same results as you -- sys.argv worked, sys.argx didn't (I'm assuming that argx is just a dummy, right?).

I'm not really sophisticated enough to understand what you're asking re: the sys identifier.  Are you suggesting that the identifier that I'm using is not pointing at the correct sys file?  In any event, I searched for sys.py, and didn't find anything.
0
 
cshore12Author Commented:
Yes, you're right about the service.py file -- but the problem still remains that the program does not recognize the test1 module, despite the fact that they reside in the same directory.
0
 
cshore12Author Commented:
pepr -- I've gone around and around on this, and now I'm back to this argv[0] error.  The service/test1 issue is resolved, and I can capture the user-entered value if I can get past this error.

I've gone into Tkinter.py and hard coded several candidates (Tkinter.py, Clip_addin.py, and clippingValue.py) for the baseName (line 1680).  Clip_addin.py is the name of the main module, and clippingValue.py is the Tkinter module.  In each case my application failed.  

I'm sorry, but I really don't understand what you're suggesting about the sys module.  It sounds like you're saying that, if I can't find an external sys.py, then there's really nothing more we can do -- is that correct?
0
 
peprCommented:
I will try to be more clear about the sys module. The sys module is the standard Python module. Moreover, it is built-in module (part of the interpreter). This way, there is no sys.py file. More below.

Other standard modules may have their .py file in the "C:\Python27\Lib". If you give your own module the same name, it can mask the existence of the standard module (i.e. when use import xxx and the xxx.py is both system and your file name). Because of that I thought you may have some sys.py (named unintentionaly) somewhere as a part of your solution. However, it cannot be done with the built-in sys module -- Python prefers its own module as it does not try to search for the sys.py file. You can try the following test...

Let's create our own sys.py with the content:
argv = 'This is my own sys.arv'

Open in new window


Let e.py file in the same subdirectory contains
import sys
print sys.argv

Open in new window

If our own sys module were prefered, it would print This is my own sys.arv. However, the e.py prints:
c:\tmp\___python\cshore12\Q_28241147>py e.py
['e.py']

Open in new window


This way, the reason for the problem is hidden in other sources -- probably in ArcGIS source, possibly in your own sources (I do not know). If--for example--the del sys.argv command were executed somewhere, it would produce the same error as you observe. Anyway, the problem can be caused by something else. This was the reason why I suggested to search for usage of the sys identifier in the sources of ArcGIS or in your sources.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.