TypeError: 'module object is not callable'

Amit Khemka khemkaamit at gmail.com
Mon Sep 3 11:02:35 CEST 2007


On 9/3/07, christophertidy at hotmail.com <christophertidy at hotmail.com> wrote:
> 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 have a Step.py class:
> class Step(object)
>         def __init__(self, sName):

>         "Initialise a new Step instance"
>         self.sName = sName
>         self.depSteps = []
>         self.remDepSteps = []
>         self.isCompleted = 0
>
>
> Then I have created a new file within the same directory called
> main.py:
>
> import Step
> a  = Step("magn")
>
> The following however generates the error
> Traceback (most recent call last):
>   File "main.py", line 3, in ?
>     a  = Step("magn")
> TypeError: 'module' object is not callable
>
> If anyone could help point me in the right direction, how to fix this
> problem it would be much appreciated.
> Chris

The exception is being raised as you are being confused about the
names ie: you have a class named "Step" in a module named "Step.py" .
And by importing the module the only "Step"  python sees is the module
not the names declared within the module.

To access a name defined in the module you can do :

example1:
import Step
a = Step.Step("magn") # It refers the name "Step" defined IN the module "Step"

example 2:
from Step import Step # You directly import the class ( and make the
name visible in your name space)
a = Step("magn")


Also have a look at:
http://effbot.org/zone/import-confusion.htm
http://docs.python.org/ref/import.html


Cheers,

-- 
----
Amit Khemka
website: www.onyomo.com
wap-site: www.owap.in



More information about the Python-list mailing list