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

Discussion in 'Python' started by Charles Russell, Aug 14, 2006.

  1. 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
    Charles Russell, Aug 14, 2006
    #1
    1. Advertising

  2. Charles Russell

    faulkner Guest

    works for me. do you do anything in your script besides that?


    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
    faulkner, Aug 14, 2006
    #2
    1. Advertising

  3. In <yp2Eg.1858$VQ.825@trndny05>, 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


    Don't call your file `glob.py` because then you import this module and not
    the `glob` module from the standard library.

    Ciao,
    Marc 'BlackJack' Rintsch
    Marc 'BlackJack' Rintsch, Aug 14, 2006
    #3
  4. Charles Russell

    John Machin Guest

    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>
    John Machin, Aug 14, 2006
    #4
  5. Marc 'BlackJack' Rintsch wrote:

    >
    > Don't call your file `glob.py` because then you import this module and not
    > the `glob` module from the standard library.
    >
    > Ciao,
    > Marc 'BlackJack' Rintsch


    Yes, thanks. Renaming to myglob.py solved the problem. But why does the
    conflict not occur when the code is run interactively from the python
    prompt? Somewhat related - I haven't found the magic word to invoke a
    ..py script from the python prompt (like the command "source" in csh,
    bash, tcl?) "import" runs the script, but then complains that it is not
    a module.
    Charles Russell, Aug 14, 2006
    #5
  6. John Machin wrote:

    >
    > 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"
    >


    Thanks. Another newby question: __name__ and __file__ appear to be
    predefined variables. To look up their meaning in the manual, is there
    some method less clumsy than grepping the whole collection of .html
    source files? I can't find any comprehensive index.
    Charles Russell, Aug 14, 2006
    #6
  7. Charles Russell wrote:

    But why does the
    > conflict not occur when the code is run interactively from the python
    > prompt?


    Because, I now realize, I had not yet created glob.py when I tried that.
    Charles Russell, Aug 14, 2006
    #7
  8. Charles Russell

    John Machin Guest

    Charles Russell wrote:
    > Marc 'BlackJack' Rintsch wrote:
    >
    > >
    > > Don't call your file `glob.py` because then you import this module and not
    > > the `glob` module from the standard library.
    > >
    > > Ciao,
    > > Marc 'BlackJack' Rintsch

    >
    > Yes, thanks. Renaming to myglob.py solved the problem. But why does the
    > conflict not occur when the code is run interactively from the python
    > prompt?


    It does for me on Windows -- see below --because '' (representing the
    cwd) is injected at the front of sys.path. *xMMV of course. Or perhaps
    when you ran it your cwd was some other directory.

    C:\junk>dir glob*
    [snip]
    15/08/2006 04:28 AM 662 glob.py
    15/08/2006 04:28 AM 592 glob.pyc
    [snip]
    C:\junk>python
    Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)]
    on win32
    Type "help", "copyright", "credits" or "license" for more information.
    |>>> import glob
    *** Aarrgghh!! I'm being imported as glob
    glob was imported from glob.pyc
    glob.glob is <type 'module'>
    glob.glob was imported from glob.pyc
    (glob.glob is glob) is True
    --- end of import
    |>>>

    Cheers,
    John
    John Machin, Aug 14, 2006
    #8
  9. In <0E5Eg.19638$yE1.18048@trndny02>, Charles Russell wrote:

    > Another newby question: __name__ and __file__ appear to be
    > predefined variables. To look up their meaning in the manual, is there
    > some method less clumsy than grepping the whole collection of .html
    > source files? I can't find any comprehensive index.


    Here's the index of the reference manual:

    http://docs.python.org/ref/genindex.html

    Ciao,
    Marc 'BlackJack' Rintsch
    Marc 'BlackJack' Rintsch, Aug 15, 2006
    #9
  10. Charles Russell wrote:
    I haven't found the magic word to invoke a
    > .py script from the python prompt (like the command "source" in csh,
    > bash, tcl?)


    Seems to be execfile()
    Charles Russell, Aug 15, 2006
    #10
  11. Marc 'BlackJack' Rintsch wrote:

    > Here's the index of the reference manual:
    >
    > http://docs.python.org/ref/genindex.html
    >

    Thanks. When I go up a level from there, I find a pointer to the index
    right at the bottom of the table of contents, which I had overlooked.
    Charles Russell, Aug 15, 2006
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gary Wessle
    Replies:
    1
    Views:
    19,720
    John Machin
    Apr 28, 2006
  2. Replies:
    3
    Views:
    411
    Gabriel Genellina
    Mar 17, 2007
  3. Bruno Desthuilliers

    TypeError: 'module object is not callable'

    Bruno Desthuilliers, Sep 2, 2007, in forum: Python
    Replies:
    34
    Views:
    1,205
    Ryan Ginstrom
    Sep 5, 2007
  4. Forafo San
    Replies:
    5
    Views:
    435
    Forafo San
    Aug 12, 2011
  5. Ulrich Eckhardt
    Replies:
    6
    Views:
    105
    Peter Otten
    Jul 12, 2013
Loading...

Share This Page