Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help! - problem with import random

Status
Not open for further replies.

djones7936

Programmer
Jul 20, 2002
24
US
Hello,

I'm new to Python.
When I open a fresh shell and type:

>>> import random
>>> print random.random()

the code works.

If I put those same two lines into a .py file and try to run the program (both from the command line or from a new window in Python's shell), I get an error message.

Traceback (most recent call last):
File &quot;<string>&quot;, line 1, in ?
File &quot;F:\USER\Programming\Python\learning\random.py&quot;, line 2, in ?
print random.random()
TypeError: 'module' object is not callable

Why does it do that/how can I get around that?

Python is installed at F:\Program Files\Python22
the code is stored in F:\User\programming\python\learning\random.py

Help!
-Dave
 
I figured it out!

Your py project files can't have the same name as Python's module files! (Heavenly hymn) Ooops

I tried running a code example from the Internet in the same folder that my random project (random.py) was residing. The example tried importing my project(random.py) rather than Python's random.py.

Silly me. I renamed my random.py to random2.py, all the random code in the folder started running again! I guess I short curcuited it. -Dave
(Do I get a gold star?)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top