import random error

R

Rob B

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
 
R

Russell Blau

Rob B said:
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).
 
A

A.M. Kuchling

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
 

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
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top