473,320 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
Aug 14 '06 #1
10 11448
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
Aug 14 '06 #2
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
Aug 14 '06 #3
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>

Aug 14 '06 #4
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.
Aug 14 '06 #5
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.
Aug 14 '06 #6
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.
Aug 14 '06 #7

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

Aug 14 '06 #8
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
Aug 15 '06 #9
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()
Aug 15 '06 #10
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.
Aug 15 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Atul Kshirsagar | last post by:
Hello, I am using Python 2.3.2 with a C++ extention DLL in muti-threaded environment. 1. For each new thread I create a separate sub-interpreter. 2. Each thread executes multiple python...
7
by: ‘5ÛHH575-UAZWKVVP-7H2H48V3 | last post by:
(see end of message for example code) When an instance has a dynamically assigned instance method, deepcopy throws a TypeError with the message "TypeError: instancemethod expected at least 2...
5
by: sophie_newbie | last post by:
OK this might seem like a retarded question, but what is the difference between a library and a module? If I do: import string am I importing a module or a library? And if i do...
5
by: Randall Parker | last post by:
Using Python 2.4.2 on Windows 2000 in SPE. Getting: TypeError: 'str' object is not callable on this line: TmpErrMsg1 = "State machine %s " (StateMachineName) In Winpdb 1.0.6 the...
1
by: Gary Wessle | last post by:
dear python users I am not sure why I am getting **************************************************************** Traceback (most recent call last): File "my.py", line 3, in ?...
2
by: AWasilenko | last post by:
I'm trying to test a few different approaches to displaying pages via Cherrypy and I'm not having much luck. Here is my code so far: import sys, cherrypy, html class Root: @cherrypy.expose...
33
by: christophertidy | last post by:
Hi I am new to Python and have recieved this error message when trying to instantiate an object from a class from another file within the same directory and wondered what I have done wrong. I...
18
by: Charlie of Bolton | last post by:
Hi, everybody, Did work hard on this one, as I`m a newbies... I did write the entire below script... This script is suppose to ping: a primary IP (only one), (entered manually w raw-input) ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.