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

How did I screw this up?

2 views
Skip to first unread message

Widgeteye

unread,
Jul 23, 2004, 11:55:03 AM7/23/04
to

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

Peter Otten

unread,
Jul 23, 2004, 12:15:40 PM7/23/04
to
Widgeteye wrote:

> 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

Widgeteye

unread,
Jul 23, 2004, 12:24:22 PM7/23/04
to


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

Christopher T King

unread,
Jul 23, 2004, 12:25:18 PM7/23/04
to
On Fri, 23 Jul 2004, Widgeteye wrote:

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

0 new messages