ImportError: cannot import name

后端 未结 4 2079
余生分开走
余生分开走 2020-12-08 14:24

I have two files app.py and mod_login.py

app.py

from flask import Flask
from mod_login import mod_login

app = Flask(__name         


        
相关标签:
4条回答
  • 2020-12-08 14:34

    This can also happen if you've been working on your scripts and functions and have been moving them around (i.e. changed the location of the definition) which could have accidentally created a looping reference.

    You may find that the situation is solved if you just reset the iPython kernal to clear any old assignments:

    %reset
    

    or menu->restart terminal

    0 讨论(0)
  • 2020-12-08 14:43

    Instead of using local imports, you may import the entire module instead of the particular object. Then, in your app module, call mod_login.mod_login

    app.py

    from flask import Flask
    import mod_login
    
    # ...
    
    do_stuff_with(mod_login.mod_login)
    

    mod_login.py

    from app import app
    
    mod_login = something
    
    0 讨论(0)
  • 2020-12-08 14:48

    The problem is that you have a circular import: in app.py

    from mod_login import mod_login
    

    in mod_login.py

    from app import app
    

    This is not permitted in Python. See Circular import dependency in Python for more info. In short, the solution are

    • either gather everything in one big file
    • delay one of the import using local import
    0 讨论(0)
  • 2020-12-08 14:55

    When this is in a python console if you update a module to be able to use it through the console does not help reset, you must use a

    import importlib
    

    and

    importlib.reload (*module*)
    

    likely to solve your problem

    0 讨论(0)
提交回复
热议问题