1
Last login: Tue Nov  5 15:13:08 on ttys000 
Preetis-MacBook-Pro:~ preetigupta25$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
Preetis-MacBook-Pro:~ preetigupta25$ /Library/Frameworks/Python.framework/Versions/2.7/bin/python
Python 2.7.6rc1 (v2.7.6rc1:4913d0e9be30+, Oct 27 2013, 20:52:11) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> ^D
Preetis-MacBook-Pro:~ preetigupta25$ /usr/bin/python
Python 2.7.6rc1 (v2.7.6rc1:4913d0e9be30+, Oct 27 2013, 20:52:11) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> ^D
Preetis-MacBook-Pro:~ preetigupta25$ cd /Users/preetigupta25/Documents/lab\ work/Economic\ modelling/src
-bash: cd: /Users/preetigupta25/Documents/lab work/Economic modelling/src: No such file or directory
Preetis-MacBook-Pro:~ preetigupta25$ cd /Users/preetigupta25/Documents/lab\ work/Economic\ modelling/Preeti_model/src
Preetis-MacBook-Pro:src preetigupta25$ ./model.py
Traceback (most recent call last):
  File "./model.py", line 5, in <module>
    from random import generate_random_no
  File "/Users/preetigupta25/Documents/lab work/Economic modelling/Preeti_model/src/random.py", line 4, in <module>
    from random import random
ImportError: cannot import name random
Preetis-MacBook-Pro:src preetigupta25$ python
Python 2.7.6rc1 (v2.7.6rc1:4913d0e9be30+, Oct 27 2013, 20:52:11) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "random.py", line 4, in <module>
    from random import random
ImportError: cannot import name random
>>> 

I have Python 2.7.6 on my Mac OSX. I am trying to include random module in my python script which is not working.

If you see above, import random works fine on python prompt. But as soon as I run my script :( it can't find random module and after that I can not import random on python prompt as well.
My script is having #!/usr/bin/python as the first line.

import os
import sys

from random import random

class generate_random_no:
    def __init__(self):
        #rand = random()
        pass
    def get_random_number(self):
        return randint()

    def get_random_number(self, upperbound):
        return randrange(upperbound)
2
  • It looks like the problem is in from random input ... Are you sure module random contains generate_random_no at all? I would suggest installing ipython, that way you can browse through the submodules of any module using TAB - it helps a lot.
    – Sleepyhead
    Nov 6, 2013 at 3:37
  • Try in the command line: from random input random Does it work?
    – Sleepyhead
    Nov 6, 2013 at 3:38

1 Answer 1

6

Avoid filename like random.py. It will shadow standard library module random.

Your Answer

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