Can't import modules

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Sep 30 16:21:01 EDT 2012


On Sun, 30 Sep 2012 12:42:37 -0700, Peter Farrell wrote:

> Hello!
> 
> I'm still new to Python, so here's another easy one. After I save
> something I've done as a .py file, how do I import it into something
> else I work on? Every time I try to import something other than turtle
> or math, I get this error message:
> 
> 'module' object is not callable
> 
> What am I doing wrong?


I would say that the two things you are doing wrong are, firstly, not 
posting the ENTIRE error message, including the full traceback, and most 
importantly, not paying attention to what Python is trying to tell you.

The full traceback gives import information. For example:

py> import foo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'foo'


Note carefully that Python tells you the *kind* of error made: an IMPORT 
error, as well as a specific error message, namely that there is no 
module called "foo". Python will often print the actual line causing the 
error as well.

In your case, my guess is that this is your error:

py> import math
py> math(123)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable


Notice that the import succeeds. So it is *incorrect* that you cannot 
import modules, but once you import them, you use them incorrectly! 
Python tells you exactly what went wrong, if you would only pay attention 
to it:

* it is a TYPE error, not an import error;
* modules cannot be called as if they were a function


Does this solve your problem?

If not, please:

* show us the ACTUAL code you are using
* show us the full traceback, not just the error message
* and don't expect us to guess what you are doing


Thank you.


-- 
Steven



More information about the Python-list mailing list