import random error

Discussion in 'Python' started by Rob B, Aug 5, 2004.

  1. Rob B

    Rob B Guest

    Hello,

    I am just starting to learn Python and was writing a simple script on
    my machine (Mac OS X 10.3.4), but I can't seem to import the random
    module:

    #!/usr/bin/env python

    import random

    Results in:

    Traceback (most recent call last):
    File "./math.py", line 3, in ?
    import random
    File "/sw/lib/python2.3/random.py", line 42, in ?
    from math import log as _log, exp as _exp, pi as _pi, e as _e
    ImportError: cannot import name log

    I have such a feeling that this is either a) because I don't know what
    I'm doing or b) the python install is somehow strange. What is strange
    is that in interactive mode, import random works fine.

    Thanks in advance,

    Rob
    Rob B, Aug 5, 2004
    #1
    1. Advertising

  2. Rob B

    Russell Blau Guest

    "Rob B" <> wrote in message
    news:...
    > I am just starting to learn Python and was writing a simple script on
    > my machine (Mac OS X 10.3.4), but I can't seem to import the random
    > module:
    >
    > #!/usr/bin/env python
    >
    > import random
    >
    > Results in:
    >
    > Traceback (most recent call last):
    > File "./math.py", line 3, in ?
    > import random
    > File "/sw/lib/python2.3/random.py", line 42, in ?
    > from math import log as _log, exp as _exp, pi as _pi, e as _e
    > ImportError: cannot import name log
    >
    > I have such a feeling that this is either a) because I don't know what
    > I'm doing or b) the python install is somehow strange. What is strange
    > is that in interactive mode, import random works fine.


    Neither (a) nor (b). It is c) you have named your program "math.py" but
    "math" happens also to be the name of a module in the standard Python
    library, and the "random" module needs access to the "math" module. But
    when it searches the Python path for a module named "math" it comes upon
    your file first.

    Simple solution: call your program something else! (and make sure you
    delete math.py? from your working directory).


    --
    I don't actually read my hotmail account, but you can replace hotmail with
    excite if you really want to reach me.
    Russell Blau, Aug 5, 2004
    #2
    1. Advertising

  3. On 5 Aug 2004 14:24:57 -0700,
    Rob B <> wrote:
    > I have such a feeling that this is either a) because I don't know what
    > I'm doing or b) the python install is somehow strange. What is strange


    It's a common mistake; the 'import math' is importing your math.py, not
    Python's 'math' module. Rename your file to random-test.py or something
    else that doesn't conflict, and it should work.

    --amk
    A.M. Kuchling, Aug 5, 2004
    #3
  4. Rob B

    Rob B Guest

    Thanks! Hard to believe I spent so long on that :)
    Rob B, Aug 6, 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. Darren Clark

    Random NOt random?

    Darren Clark, Jun 24, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    442
    mikeb
    Jun 24, 2004
  2. Maziar Aflatoun

    Random not really random...

    Maziar Aflatoun, Aug 4, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    26,658
    Maziar Aflatoun
    Aug 5, 2004
  3. globalrev
    Replies:
    4
    Views:
    742
    Gabriel Genellina
    Apr 20, 2008
  4. VK
    Replies:
    15
    Views:
    1,117
    Dr J R Stockton
    May 2, 2010
  5. dbv
    Replies:
    4
    Views:
    205
Loading...

Share This Page