Flask 应用程序崩溃(很可能是由于循环导入)
Flask app crashes (most likely due to a circular import)
我在这里学习了这个教程:https://youtu.be/mISFEwojJmE?t=367。这是一个非常简单的 Flask 应用程序,用于为站点创建用户登录和注册页面。教程中到目前为止一切正常:在我项目的脚本中导入对象时出现此错误。
错误:
flask.cli.NoAppException
flask.cli.NoAppException: While importing "app", an ImportError was raised:
Traceback (most recent call last):
File "c:\users\dejar\onedrive\coding\themes & applications[=10=]2 static template\install\env\lib\site-packages\flask\cli.py", line 240, in locate_app
__import__(module_name)
File "C:\Users\dejar\OneDrive\Coding\Themes & Applications[=10=]2 Static Template\Install\app.py", line 6, in <module>
from account import routes
File "C:\Users\dejar\OneDrive\Coding\Themes & Applications[=10=]2 Static Template\Install\account\routes.py", line 5, in <module>
from app import app, now
ImportError: cannot import name 'app' from partially initialized module 'app' (most likely due to a circular import) (C:\Users\dejar\OneDrive\Coding\Themes & Applications[=10=]2 Static Template\Install\app.py)
Traceback (most recent call last)
File "C:\Users\dejar\OneDrive\Coding\Themes & Applications[=10=]2 Static Template\Install\app.py", line 6, in <module>
from account import routes
File "C:\Users\dejar\OneDrive\Coding\Themes & Applications[=10=]2 Static Template\Install\account\routes.py", line 5, in <module>
from app import app, now
During handling of the above exception, another exception occurred:
File "C:\Users\dejar\OneDrive\Coding\Themes & Applications[=10=]2 Static Template\Install\env\Lib\site-packages\flask\cli.py", line 338, in __call__
self._flush_bg_loading_exception()
File "C:\Users\dejar\OneDrive\Coding\Themes & Applications[=10=]2 Static Template\Install\env\Lib\site-packages\flask\cli.py", line 326, in _flush_bg_loading_exception
reraise(*exc_info)
File "C:\Users\dejar\OneDrive\Coding\Themes & Applications[=10=]2 Static Template\Install\env\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\dejar\OneDrive\Coding\Themes & Applications[=10=]2 Static Template\Install\env\Lib\site-packages\flask\cli.py", line 314, in _load_app
self._load_unlocked()
File "C:\Users\dejar\OneDrive\Coding\Themes & Applications[=10=]2 Static Template\Install\env\Lib\site-packages\flask\cli.py", line 330, in _load_unlocked
self._app = rv = self.loader()
File "C:\Users\dejar\OneDrive\Coding\Themes & Applications[=10=]2 Static Template\Install\env\Lib\site-packages\flask\cli.py", line 388, in load_app
app = locate_app(self, import_name, name)
File "C:\Users\dejar\OneDrive\Coding\Themes & Applications[=10=]2 Static Template\Install\env\Lib\site-packages\flask\cli.py", line 245, in locate_app
raise NoAppException(
flask.cli.NoAppException: While importing "app", an ImportError was raised:
Traceback (most recent call last):
File "c:\users\dejar\onedrive\coding\themes & applications[=10=]2 static template\install\env\lib\site-packages\flask\cli.py", line 240, in locate_app
__import__(module_name)
File "C:\Users\dejar\OneDrive\Coding\Themes & Applications[=10=]2 Static Template\Install\app.py", line 6, in <module>
from account import routes
File "C:\Users\dejar\OneDrive\Coding\Themes & Applications[=10=]2 Static Template\Install\account\routes.py", line 5, in <module>
from app import app, now
ImportError: cannot import name 'app' from partially initialized module 'app' (most likely due to a circular import) (C:\Users\dejar\OneDrive\Coding\Themes & Applications[=10=]2 Static Template\Install\app.py)
The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.
To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.
You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:
dump() shows all variables in the frame
dump(obj) dumps all that's known about the object
我没有发布我的实际应用程序,而是返回并复制了教程中到目前为止他的应用程序,该教程适用于他的 Mac。但是抛出与我的实际应用程序相同的错误,运行 在我的电脑上。我还尝试以管理员身份从 VS 代码和命令行使用我的环境。这里是:
文件结构
C:.Root directory for the project
| app.py
| tree.txt
|
+---env
......more evn files and folders
|
+---static
| +---css
| | normalize.css
| | styles.css
| |
| \---js
| jquery.js
|
+---templates
| base.html
| dashboard.html
| home.html
|
+---user
| | models.py
| | routes.py
| | __init__.py
| |
| \---__pycache__
| routes.cpython-38.pyc
| __init__.cpython-38.pyc
|
\---__pycache__
app.cpython-38.pyc
app.py
from flask import Flask, render_template
# account routes
from user import routes
# create and configure the app, an instance of Flask
app = Flask(__name__)
@app.route("/")
def home():
return render_template("home.html")
@app.route("/dashboard/")
def dashboard():
return render_template("dashboard.html")
models.py
from flask import Flask, jsonify
class User:
def signup(self):
user = {"_id": "", "name": "", "email": "", "password": ""}
return jsonify(user), 200
# if successful, return status code 200 in json format
routes.py
from flask import Flask, render_template
# import the instance of app from app.py
# from file/module import instance
from app import app
# from folder.file import class
from user.models import User
@app.route("/user/signup", methods=["GET"])
def signup():
return User().signup()
注意:init.py文件为空
错误消息 cannot import name 'app' from partially initialized module 'app'
的意思是,当 Python 编译一个文件时,它需要编译另一个文件——要导入的模块。但是,第二个文件 also 需要导入才能读取,这是原始的第一个文件。所以python不能编译一个而不编译另一个。
最好的解决方案是在 app.py 文件的 末尾 导入 app.routes,如下所示:
from flask import Flask, render_template #imports at the top
#the rest of your code
from app import routes
这应该可以解决问题。
将 from app import app
放在 routes.py 的顶部。请记住,在您的应用程序目录中没有 init.py 可能会导致导入困难。
编辑:
正如以前lyanakin 所说,import 语句不必位于文件末尾。相反,它可以在 app
初始化后的任何位置定位:
app.py:
# create and configure the app, an instance of Flask
app = Flask(__name__)
# account routes
from user import routes
我在这里学习了这个教程:https://youtu.be/mISFEwojJmE?t=367。这是一个非常简单的 Flask 应用程序,用于为站点创建用户登录和注册页面。教程中到目前为止一切正常:在我项目的脚本中导入对象时出现此错误。
错误:
flask.cli.NoAppException
flask.cli.NoAppException: While importing "app", an ImportError was raised:
Traceback (most recent call last):
File "c:\users\dejar\onedrive\coding\themes & applications[=10=]2 static template\install\env\lib\site-packages\flask\cli.py", line 240, in locate_app
__import__(module_name)
File "C:\Users\dejar\OneDrive\Coding\Themes & Applications[=10=]2 Static Template\Install\app.py", line 6, in <module>
from account import routes
File "C:\Users\dejar\OneDrive\Coding\Themes & Applications[=10=]2 Static Template\Install\account\routes.py", line 5, in <module>
from app import app, now
ImportError: cannot import name 'app' from partially initialized module 'app' (most likely due to a circular import) (C:\Users\dejar\OneDrive\Coding\Themes & Applications[=10=]2 Static Template\Install\app.py)
Traceback (most recent call last)
File "C:\Users\dejar\OneDrive\Coding\Themes & Applications[=10=]2 Static Template\Install\app.py", line 6, in <module>
from account import routes
File "C:\Users\dejar\OneDrive\Coding\Themes & Applications[=10=]2 Static Template\Install\account\routes.py", line 5, in <module>
from app import app, now
During handling of the above exception, another exception occurred:
File "C:\Users\dejar\OneDrive\Coding\Themes & Applications[=10=]2 Static Template\Install\env\Lib\site-packages\flask\cli.py", line 338, in __call__
self._flush_bg_loading_exception()
File "C:\Users\dejar\OneDrive\Coding\Themes & Applications[=10=]2 Static Template\Install\env\Lib\site-packages\flask\cli.py", line 326, in _flush_bg_loading_exception
reraise(*exc_info)
File "C:\Users\dejar\OneDrive\Coding\Themes & Applications[=10=]2 Static Template\Install\env\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\dejar\OneDrive\Coding\Themes & Applications[=10=]2 Static Template\Install\env\Lib\site-packages\flask\cli.py", line 314, in _load_app
self._load_unlocked()
File "C:\Users\dejar\OneDrive\Coding\Themes & Applications[=10=]2 Static Template\Install\env\Lib\site-packages\flask\cli.py", line 330, in _load_unlocked
self._app = rv = self.loader()
File "C:\Users\dejar\OneDrive\Coding\Themes & Applications[=10=]2 Static Template\Install\env\Lib\site-packages\flask\cli.py", line 388, in load_app
app = locate_app(self, import_name, name)
File "C:\Users\dejar\OneDrive\Coding\Themes & Applications[=10=]2 Static Template\Install\env\Lib\site-packages\flask\cli.py", line 245, in locate_app
raise NoAppException(
flask.cli.NoAppException: While importing "app", an ImportError was raised:
Traceback (most recent call last):
File "c:\users\dejar\onedrive\coding\themes & applications[=10=]2 static template\install\env\lib\site-packages\flask\cli.py", line 240, in locate_app
__import__(module_name)
File "C:\Users\dejar\OneDrive\Coding\Themes & Applications[=10=]2 Static Template\Install\app.py", line 6, in <module>
from account import routes
File "C:\Users\dejar\OneDrive\Coding\Themes & Applications[=10=]2 Static Template\Install\account\routes.py", line 5, in <module>
from app import app, now
ImportError: cannot import name 'app' from partially initialized module 'app' (most likely due to a circular import) (C:\Users\dejar\OneDrive\Coding\Themes & Applications[=10=]2 Static Template\Install\app.py)
The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.
To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.
You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:
dump() shows all variables in the frame
dump(obj) dumps all that's known about the object
我没有发布我的实际应用程序,而是返回并复制了教程中到目前为止他的应用程序,该教程适用于他的 Mac。但是抛出与我的实际应用程序相同的错误,运行 在我的电脑上。我还尝试以管理员身份从 VS 代码和命令行使用我的环境。这里是:
文件结构
C:.Root directory for the project
| app.py
| tree.txt
|
+---env
......more evn files and folders
|
+---static
| +---css
| | normalize.css
| | styles.css
| |
| \---js
| jquery.js
|
+---templates
| base.html
| dashboard.html
| home.html
|
+---user
| | models.py
| | routes.py
| | __init__.py
| |
| \---__pycache__
| routes.cpython-38.pyc
| __init__.cpython-38.pyc
|
\---__pycache__
app.cpython-38.pyc
app.py
from flask import Flask, render_template
# account routes
from user import routes
# create and configure the app, an instance of Flask
app = Flask(__name__)
@app.route("/")
def home():
return render_template("home.html")
@app.route("/dashboard/")
def dashboard():
return render_template("dashboard.html")
models.py
from flask import Flask, jsonify
class User:
def signup(self):
user = {"_id": "", "name": "", "email": "", "password": ""}
return jsonify(user), 200
# if successful, return status code 200 in json format
routes.py
from flask import Flask, render_template
# import the instance of app from app.py
# from file/module import instance
from app import app
# from folder.file import class
from user.models import User
@app.route("/user/signup", methods=["GET"])
def signup():
return User().signup()
注意:init.py文件为空
错误消息 cannot import name 'app' from partially initialized module 'app'
的意思是,当 Python 编译一个文件时,它需要编译另一个文件——要导入的模块。但是,第二个文件 also 需要导入才能读取,这是原始的第一个文件。所以python不能编译一个而不编译另一个。
最好的解决方案是在 app.py 文件的 末尾 导入 app.routes,如下所示:
from flask import Flask, render_template #imports at the top
#the rest of your code
from app import routes
这应该可以解决问题。
将 from app import app
放在 routes.py 的顶部。请记住,在您的应用程序目录中没有 init.py 可能会导致导入困难。
编辑:
正如以前lyanakin 所说,import 语句不必位于文件末尾。相反,它可以在 app
初始化后的任何位置定位:
app.py:
# create and configure the app, an instance of Flask
app = Flask(__name__)
# account routes
from user import routes