TypeError: 'module' object is not callable (newby question)

John Machin sjmachin at lexicon.net
Mon Aug 14 14:42:48 EDT 2006


Charles Russell wrote:
> Why does this work from the python prompt, but fail from a script?
> How does one make it work from a script?
>
> #! /usr/bin/python
> import glob
> # following line works from python prompt; why not in script?
> files=glob.glob('*.py')
> print files
>
> Traceback (most recent call last):
>    File "./glob.py", line 2, in ?
>      import glob
>    File "/home/cdr/python/glob.py", line 5, in ?
>      files=glob.glob('*.py')
> TypeError: 'module' object is not callable

Short answer: Change the name of your script file.

Long answer:

<humour>
It is attempting to emulate the mythical ooloo bird, which is allegedly
capable of evading would-be predators by vanishing up its own
fundamental orifice. This topological exploit won't be available in
Python until the as yet still mythical Python 3000.
</humour>

Contemplate the following:

C:\junk>type glob.py
if __name__ == "__main__":
    print "*** Being run as a script ..."
    import glob
    print "glob was imported from", glob.__file__
    print "glob.glob is", type(glob.glob)
    print "glob.glob was imported from", glob.glob.__file__
    print "(glob.glob is glob) is", glob.glob is glob
    print "--- end of script"
else:
    print "*** Aarrgghh!! I'm being imported as", __name__
    import glob
    print "glob was imported from", glob.__file__
    print "glob.glob is", type(glob.glob)
    print "glob.glob was imported from", glob.glob.__file__
    print "(glob.glob is glob) is", glob.glob is glob
    print "--- end of import"

C:\junk>glob.py
*** Being run as a script ...
*** Aarrgghh!! I'm being imported as glob
glob was imported from C:\junk\glob.pyc
glob.glob is <type 'module'>
glob.glob was imported from C:\junk\glob.pyc
(glob.glob is glob) is True
--- end of import
glob was imported from C:\junk\glob.pyc
glob.glob is <type 'module'>
glob.glob was imported from C:\junk\glob.pyc
(glob.glob is glob) is True
--- end of script

HTH,
John

C:\junk>




More information about the Python-list mailing list