TypeError: 'module' object is not callable

F

Forafo San

I wrote a class, Univariate, that resides in a directory that is in my PYTHONPATH. I'm able to import that class into a *.py file. However when I try to instantiate an object with that class like:

x = Univariate(a) # a is a list that is expected by the Univariate class

python raises the TypeError: 'module' object is not callable. If I embed the code of the Univariate class in my *.py file, there is no problem. Also, when the class is imported and I do a

print dir(Univariate)

it does not print all the methods that are in the class, while if the class code appears in my *.py file, all the methods are available and a list with the correct methods are printed.

What gives?

Thanks in advance.
======================
FreeBSD 7.2 machine with python version 2.5.6
 
J

John Gordon

In said:
I wrote a class, Univariate, that resides in a directory that is in my PYTHONPATH. I'm able to import that class into a *.py file. However when I try to instantiate an object with that class like:
x = Univariate(a) # a is a list that is expected by the Univariate class
python raises the TypeError: 'module' object is not callable. If I embed the code of the Univariate class in my *.py file, there is no problem. Also, when the class is imported and I do a

It's difficult to diagnose this problem when you haven't shown us the
code for Univariate.
 
D

David Robinow

I wrote a class, Univariate, that resides in a directory that is in my PYTHONPATH. I'm able to >import that class into a *.py file. However when I try to instantiate an object with that class like:

What makes you think you're able to import that class?
x = Univariate(a)             # a is a list that is expected by the Univariate class

python raises the TypeError: 'module' object is not callable.  If I embed the code of the Univariate >class in my *.py file, there is no problem.  Also, when the class is imported and I do a

print dir(Univariate)

it does not print all the methods that are in the class, while if the class code appears in my *.py >file, all the methods are available and a list with the correct methods are printed.

It appears that you are importing the "module" Univariate from the
file Univariate.py
If you want to instantiate the "class" Univariate contained in the
"module" Univariate, try

x = Univariate.Univariate(a)

In the future, please try to include more of your code.
 
M

MRAB

I wrote a class, Univariate, that resides in a directory that is in my PYTHONPATH. I'm able to import that class into a *.py file. However when I try to instantiate an object with that class like:

x = Univariate(a) # a is a list that is expected by the Univariate class

python raises the TypeError: 'module' object is not callable. If I embed the code of the Univariate class in my *.py file, there is no problem. Also, when the class is imported and I do a

print dir(Univariate)

it does not print all the methods that are in the class, while if the class code appears in my *.py file, all the methods are available and a list with the correct methods are printed.

What gives?
I think you mat be confusing the class with the module.

When you write:

import Univariate

you're importing the module.

If the module is called "Univariate" and the class within the module is
called "Univariate" then you should either write:

import Univariate
x = Univariate.Univariate(a) # the class Univariate in the module
Univariate

or:

from Univariate import Univariate
x = Univariate(a)

Incidentally, it's recommended that module names use lowercase, so that
would be:

import univariate
x = univariate.Univariate(a)

or:

from univariate import Univariate
 
F

Forafo San

I think you mat be confusing the class with the module.

When you write:

import Univariate

you're importing the module.

If the module is called "Univariate" and the class within the module is
called "Univariate" then you should either write:

import Univariate
x = Univariate.Univariate(a) # the class Univariate in the module
Univariate

or:

from Univariate import Univariate
x = Univariate(a)

Incidentally, it's recommended that module names use lowercase, so that
would be:

import univariate
x = univariate.Univariate(a)

or:

from univariate import Univariate

Thank you all for your replies. When I do a

from Univariate import Univariate

the TypeError disappears and everything is fine. Clearly this was an error that a newbie such as myself is likely to make because of little experience with Python. However, this isn't something I'm likely to forget.

I will also adopt the style recommendations. Thanks, again.
 
F

Forafo San

I think you mat be confusing the class with the module.

When you write:

import Univariate

you're importing the module.

If the module is called "Univariate" and the class within the module is
called "Univariate" then you should either write:

import Univariate
x = Univariate.Univariate(a) # the class Univariate in the module
Univariate

or:

from Univariate import Univariate
x = Univariate(a)

Incidentally, it's recommended that module names use lowercase, so that
would be:

import univariate
x = univariate.Univariate(a)

or:

from univariate import Univariate

Thank you all for your replies. When I do a

from Univariate import Univariate

the TypeError disappears and everything is fine. Clearly this was an error that a newbie such as myself is likely to make because of little experience with Python. However, this isn't something I'm likely to forget.

I will also adopt the style recommendations. Thanks, again.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top