0

I know this could be caused by having a self-defined python file called random.py. I have searched for it, there is not file with such names, also there are no "pyc" file with this name.

I've also tried it by just typing the command in the terminal, and it seems to work! But it doesnt work when I try to compile the file!

Any idea what the problem might be?

Thanks!

import csv
import random
from numpy import *
from scipy.stats import norm

....

index = random.randrange(length)

...
1
  • Compile the file? Have you tried to change the name of your module and see if something changes? Commented Feb 4, 2015 at 4:47

2 Answers 2

4

First, you shouldn't do this on general principles:

from numpy import *

That shadows many built-ins like any and all with numpy versions which behave very differently. But in this case, it's also causing your other problem, because there's a numpy.random which is shadowing the main random module:

>>> import random
>>> random
<module 'random' from '/usr/lib/python3.4/random.py'>
>>> from numpy import *
>>> random
<module 'numpy.random' from '/usr/local/lib/python3.4/dist-packages/numpy/random/__init__.py'>

Note as an aside that if you're going to be generating many random numbers, using np.random instead of random is likely to be faster. (import numpy as np is the standard character-saving alias.)

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah he should rename the built-ins first if he's going to do from numpy import *
1

It is not a best practice to import everything from modules. In this case, numpy seems to be interfering with random. When I change

from numpy import *

to

import numpy

the script runs. This would require you to reference anything you are using from numpy in the intervening code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.