[Tutor] Tkinter: no module named messagebox

Peter Otten __peter__ at web.de
Sun Aug 14 08:29:13 CEST 2011


brandon w wrote:

> On 08/13/2011 04:49 PM, Peter Otten wrote:

>>> How do I find the modules in Tkinter?
>>>      
>> The simplest approach is probably to explore your file system:
>>
>> Step 1: where's Tkinter?
>>
>> $ python -c 'import Tkinter, os; print os.path.dirname(Tkinter.__file__)'
>> /usr/lib/python2.6/lib-tk

> I have tried # 1. with another command but all I get is an error messages.
> 
> $ python -c 'import time, os; print os.path.dirname(time.__doc__)' #
> This one gave no output.

You typed time.__doc__ instead of time.__file__. With the latter you'd get 
an AttributeError because the time module is implemented in C.

> $ python -c 'import time, strftime, os; print
> os.path.dirname(strftime.__doc__)'
> Traceback (most recent call last):
>    File "<string>", line 1, in <module>
> ImportError: No module named strftime

Well you cannot import a module that doesn't exist...

> $ python -c 'import os; from time import strftime; print
> os.path.dirname(strftime.__file__)'
> Traceback (most recent call last):
>    File "<string>", line 1, in <module>
> AttributeError: 'builtin_function_or_method' object has no attribute
> '__file__'
> 
> $ python -c 'import time, os; print os.path.dirname(time.__file__)'
> Traceback (most recent call last):
>    File "<string>", line 1, in <module>
>    File "/usr/lib/python2.6/posixpath.py", line 119, in dirname
>      i = p.rfind('/') + 1
> AttributeError: 'NoneType' object has no attribute 'rfind'
> 
> $  python -c 'import time, os; print os.path.dirname(time.__package__)'
> more errors
> 
> I am obviously doing something wrong.

The method to find the location of a module that I gave works only for 
modules and only for modules implemented in Python. You can find both 
functions and modules implemented in Python with inspect.getsourcefile():

>>> from inspect import getsourcefile
>>> import os
>>> getsourcefile(os) # a module
'/usr/lib/python2.6/os.py'
>>> getsourcefile(os.path) # another module with an interesting filename
'/usr/lib/python2.6/posixpath.py'
>>> getsourcefile(os.walk) # a function implemented in Python
'/usr/lib/python2.6/os.py'
>>> getsourcefile(os.mkdir) # a function implemented in C
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/inspect.py", line 441, in getsourcefile
    filename = getfile(object)
  File "/usr/lib/python2.6/inspect.py", line 418, in getfile
    raise TypeError('arg is not a module, class, method, '
TypeError: arg is not a module, class, method, function, traceback, frame, 
or code object





More information about the Tutor mailing list