Can't import modules

P

Peter Farrell

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
 
B

Benjamin Kaplan

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
--

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.['Foo', '__builtins__', '__doc__', '__file__', '__name__',
'__package__', 'add2', 'x']Traceback (most recent call last):
7
 
S

Steven D'Aprano

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.
 
S

Steven D'Aprano

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.
 
H

Hans Mulder

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
 
P

Peter Farrell

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
 
D

Dave Angel

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")
 
S

Steven D'Aprano

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.
 
P

Peter Farrell

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.

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
 
S

Steven D'Aprano

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!
 
S

Steven D'Aprano

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!
 
D

Dwight Hutto

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
 
D

Dave Angel

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.
 
P

Peter Farrell

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.




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).



"""






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
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
 
P

Peter Farrell

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.




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).



"""






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
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
 
C

cjgohlke

Thanks for trying to help, everybody. Sorry I didn't post the whole errormessage. 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
 

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

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top