简体   繁体   中英

Can not import flask pycharm

from flask import Flask

app = Flask(__main__)

if __name__ == "__main__":
    app.run()

If i run this i get the following error code:

ImportError: cannot import name 'Flask' from partially initialized module 'flask' (most likely due to a circular import)

Does anybody know how to fix this?

Circular import error occurs as you are importing same module name as the name of the python script. Rename your file to resolve the issue, never use module names as your file names.

you are facing this issue because of circular import.

When Python imports a module, it checks the module registry to see if the module was already imported. If the module was already registered, Python uses that existing object from cache. The module registry is a table of modules that have been initialized and indexed by module name. This table can be accessed through sys.modules .

If it was not registered, Python finds the module, initializes it if necessary, and executes it in the new module's namespace.

In your case you are importing module flask in a script named flask . renaming the file name should resolve your issue.

to know more about circular import you can read the article:

https://stackabuse.com/python-circular-imports/

https://www.stefaanlippens.net/circular-imports-type-hints-python.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM