How did I screw this up?

Discussion in 'Python' started by Widgeteye, Jul 23, 2004.

  1. Widgeteye

    Widgeteye Guest

    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
     
    Widgeteye, Jul 23, 2004
    #1
    1. Advertising

  2. Widgeteye

    Peter Otten Guest

    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
     
    Peter Otten, Jul 23, 2004
    #2
    1. Advertising

  3. Widgeteye

    Widgeteye Guest

    On 2004-07-23, Peter Otten <> wrote:
    > 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



    Arg! That was it, now I really feel stupid.
    Thanks
     
    Widgeteye, Jul 23, 2004
    #3
  4. 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.
     
    Christopher T King, Jul 23, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wesley Groleau

    What did I screw up?

    Wesley Groleau, Aug 5, 2003, in forum: Perl
    Replies:
    6
    Views:
    1,115
    Wesley Groleau
    Aug 7, 2003
  2. Kelmen Wong
    Replies:
    0
    Views:
    346
    Kelmen Wong
    May 4, 2004
  3. Gandalf
    Replies:
    12
    Views:
    532
    Attila Feher
    Sep 16, 2003
  4. Connected
    Replies:
    2
    Views:
    826
    Liquid Technologies
    Oct 22, 2007
  5. Michael Guterl

    gem cleanup screw up...

    Michael Guterl, Jan 1, 2009, in forum: Ruby
    Replies:
    9
    Views:
    153
    Michael Guterl
    Jan 3, 2009
Loading...

Share This Page