How did I screw this up?

W

Widgeteye

I was experimenting with defining functions, as I am new to python.
And now one of the functions in the /usr/local/lib/python2.3/random.py
modules is screwed up. I didn't do anything to it. I wasn't even
in that directory. I was in the /shared/src directory where I do all
my experimenting. But I made a little program called random.py in the
/shared/src directory and put a def go(): in that file. Now when I
run any program with random.randrange() in it I get the following
error:

Traceback (most recent call last):
File "blackjack.py", line 3, in ?
import math, string, random, sys
File "/usr/local/lib/python2.3/random.py", line 7, in ?
sequences
File "/usr/local/lib/python2.3/random.py", line 4, in go
--------
AttributeError: 'module' object has no attribute 'randrange'

Up until I made the file random.py with the function go() in it
everythiing was working fine.

I deleted the file and rebooted (silly I know) but nothing seems to help.
What do I do now and what happened?????

Thanks
Widgeteye
 
P

Peter Otten

Widgeteye said:
I deleted the file and rebooted (silly I know) but nothing seems to help.
What do I do now and what happened?????

Chances are you didn't delete he compiled file random.pyc or random.pyo. Do
that and everything will be OK. No reboot needed.

Peter
 
W

Widgeteye

Chances are you didn't delete he compiled file random.pyc or random.pyo. Do
that and everything will be OK. No reboot needed.

Peter


Arg! That was it, now I really feel stupid.
Thanks
 
C

Christopher T King

I deleted the file and rebooted (silly I know) but nothing seems to help.
What do I do now and what happened?????

Initially what happened is that the 'import random' chose your script
(random.py) to import, rather than the builtin one. This is normal Python
behaviour, and for that reason you shouldn't give your script the same
name as a builtin library you want to use.

The reason why this kept happening, even after you deleted random.py, is
(I believe) because there is still a random.pyc in your /shared/src
directory. This is a compiled version of random.py that is built whenever
Python imports a module, and the .py file is newer than the .pyc file. In
your case, what I think is happening is that Python is loading the
local .pyc file (which contains your code), hitting the error, and then
printing a traceback using the only random.py it can find, i.e. the
builtin one (which makes it seem like it was overwritten). The .pyc is
not being rebuilt because it is newer than what Python thinks is the
corresponding .py file (and always will be). The solution in this case is
simply to delete the /shared/src/random.pyc file.

If that's not the case, and /usr/local/lib/python23/random.py really is
corrupted (open it and see if it contains your code first), you can
download random.py from (sorry for the long URL)
http://cvs.sourceforge.net/viewcvs.py/python/python/dist/src/Lib/random.py?rev=1.62&view=log
and use it to replace the broken random.py. *Don't* do this unless you
confirm that the .pyc file is not the culprit.

Hope this helps.
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top