Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can't import modules

62 views
Skip to first unread message

Peter Farrell

unread,
Sep 30, 2012, 3:42:37 PM9/30/12
to
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?

Thanks!

Peter Farrell
San Mateo, CA

Benjamin Kaplan

unread,
Sep 30, 2012, 4:15:11 PM9/30/12
to pytho...@python.org
> --

Well, you haven't told us what you're doing, so it's hard to say what
you're doing wrong. So I'm going to make a few guesses.

1. Your first (and possibly only other) language was Java.
2. You're making a class (let's say Foo) and putting it in a file of
the same name (Foo.py)
3. You're doing "import Foo" and then calling "Foo()" trying to
instantiate the class.

Python doesn't have that "one class per file, and the file must have
the same name as the class" rule as Java. The file defines a module,
which is an object and can have any number of objects (including
classes, because those are objects too) in it.

$ cat foo.py
class Foo(object):
pass

def add2(y) :
return y + 2
bkaplan:~ bkaplan$ python
Python 2.7.3 (default, Apr 23 2012, 10:06:17)
[GCC 4.2.1 Compatible Apple Clang 3.1 (tags/Apple/clang-318.0.58)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import foo
>>> dir(foo)
['Foo', '__builtins__', '__doc__', '__file__', '__name__',
'__package__', 'add2', 'x']
>>> foo()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
>>> foo.Foo()
<foo.Foo object at 0x100ad9d10>
>>> foo.add2(5)
7

Steven D'Aprano

unread,
Sep 30, 2012, 4:21:31 PM9/30/12
to
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

Steven D'Aprano

unread,
Sep 30, 2012, 4:21:01 PM9/30/12
to
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?


Hans Mulder

unread,
Sep 30, 2012, 4:21:51 PM9/30/12
to
On 30/09/12 21:42:37, Peter Farrell wrote:
> 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?

For starters, you're not showing us any code.

The error message suggests that you have successfully imported
a module, and you then try to use the module as if it were a
callable. That won't work: modules are not callable.

My crystal ball says that you may have been a Java programmer
in an earlier life. In Java, a file must define exactly one
class, and the class must have the same name as the file.

Python is not Java. In Python, a file may define one class,
or twenty, or none at all. To avoid confusion, do not give
any of your classes the same name as any of your files.


Hope this helps,

-- HansM


Peter Farrell

unread,
Sep 30, 2012, 8:35:02 PM9/30/12
to
Thanks for trying to help, everybody. Sorry I didn't post the whole error message. Now my problem is I just installed VPython and I'm trying to run the very first example, bounce.py which I located. I opened it and ran it in Idle. I got this message:

Traceback (most recent call last):
File "C:\Python32\Lib\site-packages\visual\examples\bounce.py", line 1, in <module>
from visual import *
File "C:\Python32\lib\site-packages\visual\__init__.py", line 1, in <module>
from .visual_all import *
File "C:\Python32\lib\site-packages\visual\visual_all.py", line 1, in <module>
from vis import version
File "C:\Python32\lib\site-packages\vis\__init__.py", line 3, in <module>
from .cvisual import (vector, dot, mag, mag2, norm, cross, rotate,
SystemError: initialization of cvisual raised unreported exception

I'm not a programmer, just a math teacher/tutor who's trying to teach my students to understand math through using something real like Python. I'll keep you posted on my progress.

Thank you in advance for your help!

Peter

Dave Angel

unread,
Sep 30, 2012, 9:16:30 PM9/30/12
to Peter Farrell, pytho...@python.org
On 09/30/2012 08:35 PM, Peter Farrell wrote:

You top-posted, which means there's no context; the message is now out
of order. Please put your responses after the parts you're quoting, or
don't bother quoting.

> Thanks for trying to help, everybody. Sorry I didn't post the whole error message. Now my problem is I just installed VPython and I'm trying to run the very first example, bounce.py which I located. I opened it and ran it in Idle. I got this message:
>
> Traceback (most recent call last):
> File "C:\Python32\Lib\site-packages\visual\examples\bounce.py", line 1, in <module>
> from visual import *
> File "C:\Python32\lib\site-packages\visual\__init__.py", line 1, in <module>
> from .visual_all import *
> File "C:\Python32\lib\site-packages\visual\visual_all.py", line 1, in <module>
> from vis import version
> File "C:\Python32\lib\site-packages\vis\__init__.py", line 3, in <module>
> from .cvisual import (vector, dot, mag, mag2, norm, cross, rotate,
> SystemError: initialization of cvisual raised unreported exception

That's a different error message than you reported at first. If you
can't even run an example supplied with the system, you'd better post a
question on the vpython forum (I presume it's available from
http://vpython.wikidot.com/system:join) Presumably the install program
is broken. Or maybe you just can't run visual from IDLE. Try running
from the terminal prompt.

I'm amazed that it uses the form: from visual_all import *
That's considered bad form. Still, it's probably not the problem. What
happens if you write a two line program:
import visual_all
print("Import succeeded")


> I'm not a programmer, just a math teacher/tutor who's trying to teach my students to understand math through using something real like Python. I'll keep you posted on my progress.
>
> Thank you in advance for your help!
>
>

--

DaveA

Steven D'Aprano

unread,
Sep 30, 2012, 9:25:29 PM9/30/12
to
On Sun, 30 Sep 2012 17:35:02 -0700, Peter Farrell wrote:

> Thanks for trying to help, everybody. Sorry I didn't post the whole
> error message. Now my problem is I just installed VPython and I'm trying
> to run the very first example, bounce.py which I located. I opened it
> and ran it in Idle. I got this message:

In general, any time you get unexpected or unusual errors in IDLE, you
should try running the same code without IDLE, just at the regular Python
prompt. Like all "Integrated Development Environments", IDLE sometimes
messes about with things in the background that very occasionally
interferes with the normal running of certain Python code, so it is
always worth taken IDLE out of the picture whenever there is a mysterious
failure.

In Windows, I *think* that if you run "python" (or perhaps "python32")
from the Start Menu, it will open an interactive prompt similar to IDLE.
Once the Python interactive interpreter has launched, just enter:

from visual import *

and see if it works. If it fails, try:

import visual


My wild guess is that VPython (whatever that is!) only works under Python
2, not Python 3, but I could be wrong.

Other than that, my advice is to check with a dedicated VPython mailing
list. Oh, and if you do find the solution, please consider reporting what
the solution is back here, for the benefit of the next person who runs
into this issue.



--
Steven

Peter Farrell

unread,
Sep 30, 2012, 9:35:50 PM9/30/12
to
Thanks again for the help. I entered the import statements you suggested into the Python Shell and they gave me the same error message. (Neither did visual_all, but of course the print statement worked.)

Since I use Python 3.2.3 I've had trouble with programs and modules designed for Python 2 and many programs don't work on my 64-bit system.

I was hoping it was a common error that newbies encounter.

Thanks again, Python fans. I'll keep you posted on my progress!

Peter

Steven D'Aprano

unread,
Sep 30, 2012, 10:15:07 PM9/30/12
to
On Sun, 30 Sep 2012 18:35:50 -0700, Peter Farrell wrote:

> Since I use Python 3.2.3 I've had trouble with programs and modules
> designed for Python 2 and many programs don't work on my 64-bit system.

While Python tries very hard to be backward compatible, the transition
from the 2.x series to the 3.x series was intentionally allowed to break
backward compatibility in certain areas.

If VPython only supports 2.x, you should ask the vendors to support at
least 3.3 or better, preferably the full 3.x series. In the meantime, you
may have to stick to the 2.x series.


> I was hoping it was a common error that newbies encounter.

Trying to run Python 2.x code in 3.x? Yes, that's common. Trying to run
VPython? Not so much.

Good luck!


--
Steven

Steven D'Aprano

unread,
Sep 30, 2012, 10:15:07 PM9/30/12
to
On Sun, 30 Sep 2012 18:35:50 -0700, Peter Farrell wrote:

> Since I use Python 3.2.3 I've had trouble with programs and modules
> designed for Python 2 and many programs don't work on my 64-bit system.

While Python tries very hard to be backward compatible, the transition
from the 2.x series to the 3.x series was intentionally allowed to break
backward compatibility in certain areas.

If VPython only supports 2.x, you should ask the vendors to support at
least 3.3 or better, preferably the full 3.x series. In the meantime, you
may have to stick to the 2.x series.


> I was hoping it was a common error that newbies encounter.

Dwight Hutto

unread,
Sep 30, 2012, 10:39:53 PM9/30/12
to Steven D'Aprano, pytho...@python.org
On Sun, Sep 30, 2012 at 10:15 PM, Steven D'Aprano
<steve+comp....@pearwood.info> wrote:
> On Sun, 30 Sep 2012 18:35:50 -0700, Peter Farrell wrote:
>
>> Since I use Python 3.2.3 I've had trouble with programs and modules
>> designed for Python 2 and many programs don't work on my 64-bit system.
>
> While Python tries very hard to be backward compatible, the transition
> from the 2.x series to the 3.x series was intentionally allowed to break
> backward compatibility in certain areas.
>
> If VPython only supports 2.x,

Naw , that's your job, plus maybe looking through the python code for
Vpython, and adding a little "from __future__ import *"
you should ask the vendors to support at
> least 3.3 or better, preferably the full 3.x series. In the meantime, you
> may have to stick to the 2.x series.
>
>
>> I was hoping it was a common error that newbies encounter.
>
> Trying to run Python 2.x code in 3.x? Yes, that's common. Trying to run
> VPython? Not so much.
>
> Good luck!
>
>
> --
> Steven
> --
> http://mail.python.org/mailman/listinfo/python-list



--
Best Regards,
David Hutto
CEO: http://www.hitwebdevelopment.com

Dwight Hutto

unread,
Sep 30, 2012, 10:48:02 PM9/30/12
to Steven D'Aprano, pytho...@python.org
Plus from What's New From Python 3", which are things you should be
able to change comes:

http://docs.python.org/release/3.0.1/whatsnew/3.0.html

Change the module yourself.

Dwight Hutto

unread,
Sep 30, 2012, 11:12:46 PM9/30/12
to Steven D'Aprano, pytho...@python.org
On Sun, Sep 30, 2012 at 10:48 PM, Dwight Hutto <dwight...@gmail.com> wrote:
> Plus from What's New From Python 3", which are things you should be
> able to change comes:
>
> http://docs.python.org/release/3.0.1/whatsnew/3.0.html
>
> Change the module yourself.
>
>

And, of course:

http://docs.python.org/library/2to3.html

Dave Angel

unread,
Sep 30, 2012, 11:18:01 PM9/30/12
to Peter Farrell, pytho...@python.org
On 09/30/2012 09:35 PM, Peter Farrell wrote:
> On Sunday, September 30, 2012 6:25:29 PM UTC-7, Steven D'Aprano wrote:
>> On Sun, 30 Sep 2012 17:35:02 -0700, Peter Farrell wrote:
>>
>>
>>
>>> Thanks for trying to help, everybody. Sorry I didn't post the whole
>>> error message. Now my problem is I just installed VPython and I'm trying
>>> to run the very first example, bounce.py which I located. I opened it
>>> and ran it in Idle. I got this message:
>>
>>
>> In general, any time you get unexpected or unusual errors in IDLE, you
>> should try running the same code without IDLE, just at the regular Python
>> prompt.
>> <SNIP>
> Thanks again for the help. I entered the import statements you suggested into the Python Shell and they gave me the same error message. (Neither did visual_all, but of course the print statement worked.)

Don't run it in the Python Shell, and don't use IDLE at all. Run your
test either at the cmd window, or from plain Python's interactive
interpreter, or maybe from VIDLE.

>From the official download page:
http://www.vpython.org/contents/download_windows.html


"""How to run VPython

*

Start the program editor with the "VIDLE for Python" shortcut on the
desktop
or on the Start menu.

*

Open an example program -- for example, bounce2.py.

*

Press F5 to run (or use the Run menu).

"""


> Since I use Python 3.2.3 I've had trouble with programs and modules designed for Python 2 and many programs don't work on my 64-bit system.
>
> I was hoping it was a common error that newbies encounter.
>
> Thanks again, Python fans. I'll keep you posted on my progress!
VPython has separate versions for Python 2.7 and 3.2 Hopefully you
downloaded the 3.2 version, since you're running it in c:\python32

Also, these are for the 32bit version of CPython. So double-check which
one you've got installed.



--

DaveA

Peter Farrell

unread,
Oct 1, 2012, 12:44:30 AM10/1/12
to Peter Farrell, pytho...@python.org, d...@davea.name
Yes, I downloaded the 3.2 version but on 64-bit. None of the 32-bit Python programs I've installed have worked.

VPython didn't install a VIDLE icon on my desktop. An earlier version did, but then it said it couldn't find the .exe file to work!

I've sent a message to the VPython groups. I'm looking forward to using this program, when it finally works!

Thanks again,

Peter

Peter Farrell

unread,
Oct 1, 2012, 12:44:30 AM10/1/12
to comp.lan...@googlegroups.com, pytho...@python.org, d...@davea.name, Peter Farrell
On Sunday, September 30, 2012 8:19:28 PM UTC-7, Dave Angel wrote:

cjgo...@gmail.com

unread,
Oct 1, 2012, 2:10:47 AM10/1/12
to
On Sunday, September 30, 2012 5:35:02 PM UTC-7, Peter Farrell wrote:
> Thanks for trying to help, everybody. Sorry I didn't post the whole error message. Now my problem is I just installed VPython and I'm trying to run the very first example, bounce.py which I located. I opened it and ran it in Idle. I got this message:
>
>
>
> Traceback (most recent call last):
>
> File "C:\Python32\Lib\site-packages\visual\examples\bounce.py", line 1, in <module>
>
> from visual import *
>
> File "C:\Python32\lib\site-packages\visual\__init__.py", line 1, in <module>
>
> from .visual_all import *
>
> File "C:\Python32\lib\site-packages\visual\visual_all.py", line 1, in <module>
>
> from vis import version
>
> File "C:\Python32\lib\site-packages\vis\__init__.py", line 3, in <module>
>
> from .cvisual import (vector, dot, mag, mag2, norm, cross, rotate,
>
> SystemError: initialization of cvisual raised unreported exception
>
>

Works for me on win-amd64-3.2.

Do you have the numpy package installed? The cvisual extension module uses the numpy C API. The SystemError is expected if numpy is not installed.

Install numpy-MKL-1.6.2.win-amd64-py3.2.‌exe from http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy if you are using VPython-5.74.win-amd64-py3.2.‌exe.

Christoph

Mark Lawrence

unread,
Oct 1, 2012, 4:36:19 AM10/1/12
to pytho...@python.org
On 01/10/2012 02:25, Steven D'Aprano wrote:
> On Sun, 30 Sep 2012 17:35:02 -0700, Peter Farrell wrote:
>
>> Thanks for trying to help, everybody. Sorry I didn't post the whole
>> error message. Now my problem is I just installed VPython and I'm trying
>> to run the very first example, bounce.py which I located. I opened it
>> and ran it in Idle. I got this message:
>
> In general, any time you get unexpected or unusual errors in IDLE, you

>
> In Windows, I *think* that if you run "python" (or perhaps "python32")
> from the Start Menu, it will open an interactive prompt similar to IDLE.

Just python. With the arrival of pylauncher
http://www.python.org/dev/peps/pep-0397/ you can also type py [options].

--
Cheers.

Mark Lawrence.

0 new messages