user1792899
user1792899

Reputation:

Python 2.7 on Mac OSX. can not import random package Environment getting corrupted

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)

Upvotes: 1

Views: 1271

Answers (1)

falsetru
falsetru

Reputation: 369384

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

Upvotes: 6

Related Questions