1

I just started to learn python3 and wanted to make a simple program that returned the values of 5 random integers so I tried :

import random

for i in range(5):
    x=random.randint(1,6)
    print (x)

(EDIT:I changed the filename to tyxaios.py and got the following error)

Traceback (most recent call last):
  File "tyxaios.py", line 1, in <module>
    import random 
  File "/home/drunkbanana/Documents/python/random.py", line 4, in <module>
    x=random.randint(1,6)
AttributeError: module 'random' has no attribute 'randint'
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
    from apport.fileutils import likely_packaged, get_recent_crashes
  File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
    from apport.report import Report
  File "/usr/lib/python3/dist-packages/apport/report.py", line 12, in <module>
    import subprocess, tempfile, os.path, re, pwd, grp, os, time
  File "/usr/lib/python3.5/tempfile.py", line 184, in <module>
    from random import Random as _Random
  File "/home/drunkbanana/Documents/python/random.py", line 4, in <module>
    x=random.randint(1,6)
AttributeError: module 'random' has no attribute 'randint'

Original exception was:
Traceback (most recent call last):
  File "tyxaios.py", line 1, in <module>
    import random 
  File "/home/drunkbanana/Documents/python/random.py", line 4, in <module>
    x=random.randint(1,6)
AttributeError: module 'random' has no attribute 'randint'

I have installed pip in my PC (Ubuntu 16.04 LTS) and have tried pip install random but it doesn't work . I got similar errors when I tried working with the smptlib library and the datetime one .

What should I do ?

claptrap
  • 11
  • 2
  • 7
    Based on your traceback, you named your file `random`. Name it something different – Wondercricket Nov 29 '17 at 17:48
  • 1
    You've named your script `random.py`: "/home/drunkbanana/Documents/python/random.py" don't do this. Don't use names of modules. – juanpa.arrivillaga Nov 29 '17 at 17:48
  • 1
    I like naming my main script something that *can't* be a Python module name, such as including a hyphen in it, just to avoid this kind of problem. – kindall Nov 29 '17 at 17:50
  • As other said, don't name your module the same as modules you're importing. See [my answer here](https://stackoverflow.com/a/47531592/4349415) – Mike Scotty Nov 29 '17 at 17:51
  • I changed the name to tyxaios.py and got the same error(but instead wherever it said random.py it now says tyxaios.py) – claptrap Nov 29 '17 at 19:11
  • 3
    Do you have a file `random.pyc` still on the disk? Remove that. – tripleee Nov 29 '17 at 19:37

0 Answers0