Flask import error "cannot import name 'Flask'"

18,474

Rename the filename flask.py to other name; It prevents import of the third-party flask module because your file is searched first.

Also make sure there's no flask.pyc file remaining.

Share:
18,474
Matthew
Author by

Matthew

I have been programming in python for over a year, and have an interest in machine learning. I have knowledge of the popular numpy module, and like to help newbies.

Updated on June 04, 2022

Comments

  • Matthew
    Matthew almost 2 years

    I am trying to use the python module flask, and I have successfully installed it with pip. I can run the following in the command prompt python terminal and in the python shell:

        from flask import Flask
    

    It works just fine. However, when I try to run that exact same command in a saved python document, I get the following error:

        Traceback (most recent call last):
          File "C:\Users\Matthew\Desktop\flask.py", line 1, in <module>
            from flask import Flask
          File "C:\Users\Matthew\Desktop\flask.py", line 1, in <module>
            from flask import Flask
        ImportError: cannot import name 'Flask'
    

    I have browsed the internet and stack exchange looking for a solution, but non have worked.

  • Matthew
    Matthew over 6 years
    where can I find these files?
  • falsetru
    falsetru over 6 years
    @Matthew, As the error message suggest, you can find it in C:\Users\Matthew\Desktop directory. (maybe your desktop)
  • Matthew
    Matthew over 6 years
    that is the name of my python code file
  • falsetru
    falsetru over 6 years
    @Matthew, I know. The script filename flask.py prevent proper import of flask module because two name conflict; Rename the script filename to other name.
  • falsetru
    falsetru over 6 years
    @Matthew, Also make sure there's no C:\Users\Matthew\Desktop\flask.pyc remain as noted in the answer.
  • Matthew
    Matthew over 6 years
    sorry, I misinterpreted what you were saying. Now it works perfectly thanks!