Error when running mmap related function in python 2.6


I tried to run the following code from http://docs.python.org/library/mmap.html

import mmap

# write a simple example file
with open("hello.txt", "wb") as f:
    f.write("Hello Python!\n")

with open("hello.txt", "r+b") as f:
    # memory-map the file, size 0 means whole file
    map = mmap.mmap(f.fileno(), 0)
    # read content via standard file methods
    print map.readline()  # prints "Hello Python!"
    # read content via slice notation
    print map[:5]  # prints "Hello"
    # update content using slice notation;
    # note that new content must have same size
    map[6:] = " world!\n"
    # ... and read again using standard file methods
    map.seek(0)
    print map.readline()  # prints "Hello  world!"
    # close the map
    map.close()

But, I got an error.

TypeError: 'module' object is not callable

module body in mmap.py at line 9
map = mmap.mmap(f.fileno(), 0)

What's wrong with this? I use python 2.6 on Snow Leopard/Mac.


Answers:


I think you're doing something weird calling your module mmap.py, and the import is getting confused and importing the same file instead... Try changing the name to something else (preferably not a standard library module name :p)