Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Flask应用程序崩溃(最有可能是由于循环导入)_Python 3.x_Flask_Templating - Fatal编程技术网

Python 3.x Flask应用程序崩溃(最有可能是由于循环导入)

Python 3.x Flask应用程序崩溃(最有可能是由于循环导入),python-3.x,flask,templating,Python 3.x,Flask,Templating,我在这里遵循了本教程:。这是一个非常简单的Flask应用程序,用于为站点创建用户登录和注册页面。在本教程中,所有内容都适用于这一点:当在项目中的脚本中导入对象时,我会遇到此错误 错误: flask.cli.NoAppException flask.cli.NoAppException: While importing "app", an ImportError was raised: Traceback (most recent call last): File &qu

我在这里遵循了本教程:。这是一个非常简单的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\002 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\002 Static Template\Install\app.py", line 6, in <module>
    from account import routes
  File "C:\Users\dejar\OneDrive\Coding\Themes & Applications\002 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\002 Static Template\Install\app.py)

Traceback (most recent call last)
File "C:\Users\dejar\OneDrive\Coding\Themes & Applications\002 Static Template\Install\app.py", line 6, in <module>
from account import routes
File "C:\Users\dejar\OneDrive\Coding\Themes & Applications\002 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\002 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\002 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\002 Static Template\Install\env\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\dejar\OneDrive\Coding\Themes & Applications\002 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\002 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\002 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\002 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\002 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\002 Static Template\Install\app.py", line 6, in <module>
from account import routes
File "C:\Users\dejar\OneDrive\Coding\Themes & Applications\002 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\002 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
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")
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
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()
型号.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")
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
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()
routes.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")
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
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文件为空

错误消息
无法从部分初始化的模块“app”导入名称“app”
的意思是,当Python编译一个文件时,它需要编译另一个文件–要导入的模块。但是,第二个文件也需要读取导入,即原始的第一个文件。因此,python不能编译一个而不编译另一个

最好的解决方案是在app.py文件的端导入app.routes,如下所示:

从烧瓶导入烧瓶,在顶部渲染#模板#导入
#代码的其余部分
从应用程序导入路由
这应该可以解决问题

将app import app中的
保持在routes.py的顶部。请记住,应用程序目录中没有init.py可能会导致导入困难

编辑:

正如formerlyanakin所说,import语句不必位于文件的末尾。相反,它可以在初始化
app
后的任何位置进行定位:

app.py:

#创建并配置应用程序,Flask的一个实例
app=烧瓶(名称)
#客户路线
从用户导入路由

错误消息
无法从部分初始化的模块“app”导入名称“app”
的意思是,当Python编译一个文件时,它需要编译另一个文件–要导入的模块。但是,第二个文件也需要读取导入,即原始的第一个文件。因此,python不能编译一个而不编译另一个

最好的解决方案是在app.py文件的端导入app.routes,如下所示:

从烧瓶导入烧瓶,在顶部渲染#模板#导入
#代码的其余部分
从应用程序导入路由
这应该可以解决问题

将app import app中的
保持在routes.py的顶部。请记住,应用程序目录中没有init.py可能会导致导入困难

编辑:

正如formerlyanakin所说,import语句不必位于文件的末尾。相反,它可以在初始化
app
后的任何位置进行定位:

app.py:

#创建并配置应用程序,Flask的一个实例
app=烧瓶(名称)
#客户路线
从用户导入路由

您只做了一次调整就正确了。这真的很难让我理解。它不必位于我的app.py的页脚。我只需要在app.py文件中切换导入和应用实例的顺序。谢谢,格雷西亚斯。我是阿尤多·穆乔,你只做了一次调整就对了。这真的很难让我理解。它不必位于我的app.py的页脚。我只需要在app.py文件中切换导入和应用实例的顺序。谢谢,格雷西亚斯。我是阿尤多·穆乔