Python 导入错误:无法导入名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17845366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:18:46  来源:igfitidea点击:

ImportError: cannot import name

pythonflask

提问by Patrick Burns

I have two files app.pyand mod_login.py

我有两个文件app.pymod_login.py

app.py

应用程序

from flask import Flask
from mod_login import mod_login

app = Flask(__name__)
app.config.update(
    USERNAME='admin',
    PASSWORD='default'
)

mod_login.py

mod_login.py

# coding: utf8

from flask import Blueprint, render_template, redirect, session, url_for, request
from functools import wraps
from app import app

mod_login = Blueprint('mod_login', __name__, template_folder='templates')

And python return this error:

而python返回这个错误:

Traceback (most recent call last):
  File "app.py", line 2, in <module>
    from mod_login import mod_login
  File "mod_login.py", line 5, in <module>
    from app import app
  File "app.py", line 2, in <module>
    from mod_login import mod_login
ImportError: cannot import name mod_login

If I delete from app import app, code will be work, but how I can get access to app.config?

如果我删除from app import app,代码将起作用,但我如何才能访问app.config

采纳答案by hivert

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

问题是你有一个循环导入:在 app.py

from mod_login import mod_login

in mod_login.py

在 mod_login.py 中

from app import app

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

这在 Python 中是不允许的。有关详细信息,请参阅Python 中的循环导入依赖项。简而言之,解决方案是

  • either gather everything in one big file
  • delay one of the import using local import
  • 要么将所有内容都收集在一个大文件中
  • 使用本地导入延迟导入之一

回答by mjp

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:

如果您只是重置 iPython 内核以清除所有旧分配,您可能会发现情况已解决:

%reset

or menu->restart terminal

或菜单->重启终端

回答by Jivan

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

您可以导入整个模块而不是特定对象,而不是使用本地导入。然后,在您的app模块中,调用mod_login.mod_login

app.py

应用程序

from flask import Flask
import mod_login

# ...

do_stuff_with(mod_login.mod_login)

mod_login.py

mod_login.py

from app import app

mod_login = something

回答by Talles .Carvalho

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

当这是在 python 控制台中时,如果您更新模块以使其能够通过控制台使用它不会帮助重置,则必须使用

import importlib

and

importlib.reload (*module*)

likely to solve your problem

可能会解决您的问题