[Tutor] calling a module fails

Steven D'Aprano steve at pearwood.info
Tue Oct 30 02:28:52 CET 2012

On 30/10/12 12:00, richard kappler wrote:

> If, however, I save the above in a file named hungry.py, then import
> hungry, I get an error, as follows:
> import hungry
> hungry(96)
> Traceback (most recent call last):
>    File "<stdin>", line 1, in<module>
> TypeError: 'module' object is not callable
> So what am I missing? Someone do please point out the obvious. ;-)

You have a module called "hungry" and a function called "hungry" inside
that, so to reach the inner "hungry" you need to do this:

import hungry

Which is no different from:

import math

Python will never try to guess which inner function you want when you
call a module directly, not even if the inner function has the same
name as the module. You must always explicitly tell it which inner
function you want.

An alternative is this:

from hungry import hungry  # like "from math import cos"


More information about the Tutor mailing list