0

I am new to python programming and started trying to fool around with flask this morning. I installed flask using pip and it seemed to work as expected. However, when I went to the python shell and typed import flask I got the following error:

 >>> import flask
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib/python2.7/dist-packages/flask/__init__.py", line 17, 
   in <module>
         from werkzeug.exceptions import abort
      File "/usr/lib/python2.7/dist-packages/werkzeug/__init__.py", line 
    151, in <module>
       __import__('werkzeug.exceptions')
      File "/usr/lib/python2.7/dist-packages/werkzeug/exceptions.py", line 
    71, in <module>
       from werkzeug.wrappers import Response
      File "/usr/lib/python2.7/dist-packages/werkzeug/wrappers.py", line 
    27, in <module>
        from werkzeug.http import HTTP_STATUS_CODES, \
      File "/usr/lib/python2.7/dist-packages/werkzeug/http.py", line 23, 
    in <module>
        from email.utils import parsedate_tz
      File "/usr/lib/python2.7/email/utils.py", line 27, in <module>
        import random
      File "random.py", line 6, in <module>
        print(random.randint(1, 101)) 
        AttributeError: 'module' object has no attribute 'randint''

I uninstalled flask using pip and tried again. No change. I installed flask using apt, same problem I even tried it on python 2, same error message. the really weird part is that it is giving me errors from a python3 shell session I did earlier in the week - the import random for random.py part of the message at the very end. Why would it spit out messages that have nothing to do with the import flask message?

It made me think that maybe I should see if a reboot would help, but no luck there either.

How does one troubleshoot an issue like this? googling the error hasn't helped and I am running out of ideas.

Thanks in advance

Robert Baker
  • 167
  • 1
  • 1
  • 14

1 Answers1

0

From what it looks like the error message you are receiving means you have a file name random.py being invoked. This can happen if the file is in the same folder you are running the command python2 or python3.

According to the error message:

print(random.randint(1, 101))
AttributeError: 'module' object has> no attribute 'randint''

It's trying to load that file. Rename your file to something else and it should work.

If you locate random.py in linux you will see it here (depending on your versions): /usr/lib/python2.7/random.py

and

/usr/lib/python3.6/random.py

So, your random.py is causing the python command to override the base python random.py file

CodeLikeBeaker
  • 20,682
  • 14
  • 79
  • 108
  • Ah, interesting. I renamed it but that didn't work. I had to move it out of the directory altogether. Thank you very much for the help, can you tell me what's going on here? Why was I able to import other modules without the problem (os, for example)? – Robert Baker Feb 11 '19 at 19:47
  • @RobertBaker take a look at the duplicate answer link that davidism marked your question as. It goes into further explanation. Good luck! – CodeLikeBeaker Feb 11 '19 at 19:49
  • @RobertBaker this type of thing happens all the time. Sometimes it gets to the point where you bang your head for a while wondering why your code stopped working! :) – CodeLikeBeaker Feb 11 '19 at 19:49