gpt4 book ai didi

python - lask.cli.NoAppException : While importing "app", 引发了 ImportError:

转载 作者:行者123 更新时间:2023-12-04 13:09:50 25 4
gpt4 key购买 nike

1.我正在尝试构建一个flask项目并尝试将类导入models.py并将路由导入app.py。当我尝试运行该项目时,它显示了一些错误。以下是回溯的信息:

Traceback (most recent call last):
File "/Users/cheliang/Desktop/project/env/lib/python3.8/site-packages/flask/cli.py", line 240, in locate_app
__import__(module_name)
File "/Users/cheliang/Desktop/project/app.py", line 4, in <module>
from user import routes
File "/Users/cheliang/Desktop/project/user/routes.py", line 1, in <module>
from user.models import User
ImportError: cannot import name 'User' from 'user.models' (/Users/cheliang/Desktop/project/user/models.py)
我的项目的树结构:
├── app.py
├── env
── run
├── static
│   ├── css
│   │   ├── normalize.css
│   │   └── styles.css
│   └── js
│   ├── jquery.js
│   └── scripts.js
├── templates
│   ├── base.html
│   ├── dashboard.html
│   └── home.html
└── user
├── __init__.py
├── __pycache__
│   ├── __init__.cpython-38.pyc
│   ├── models.cpython-38.pyc
│   └── routes.cpython-38.pyc
├── models.py
└── routes.py
2.这是代码的详细信息:
模型.py:
from flask import Flask, jsonify
class User:
def signup(self):

user = {
"_id":"",

"name":"",
"email":"",
"password":""

}
return jsonify(user),200
这是routes.py:
from user.models import User
from flask import Flask
from app import app

@app.route('/user',methods=["GET"])
def signup(self):
return User().signup()
这是 app.py 的代码
from flask import Flask ,render_template

app = Flask(__name__)
from user import routes

@app.route('/')
def home():
return render_template('home.html')

@app.route('/dashboard')
def dashboard():
return render_template('dashboard.html')

最佳答案

对于极简的 flask 应用程序,请考虑这种结构:

project_folder
|---------- app.py
|---------- config.py
|---------- .env
|---------- requirements.txt
|---------- .flaskenv
|---------- app/
|------ routes.py
|------ models.py
|------ __init__.py
|------ forms.py
|------ templates/
|-------- home.html
|-------- base.html
|-------- dashbooard.html
|------ static/
|-------css/
|------- styles.css
|------- normalize.css
|-------js/
|------- jquery.js
|------- scripts.js
创建应用实例:
# __init__.py

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
db = SQLAlchemy(app)

from app import routes, models
使用 flask 模型:
# models.py

from app import db

class User(object):
username = db.Column(db.String())
# ...
# routes.py

from flask import render_template
from app import app
from app.models import User
from app.forms import <your-form>

@app.route('/')
def home():
return render_template('home.html')

@app.route('/dashboard')
def dashboard():
return render_template('dashboard.html')

@app.route('/user',methods=["GET"])
def signup():
# ...
user = User.query.filter_by(username=form.username.data)
最后,在 app.py 中为您的 flask 应用程序创建一个入口点。 :
from app import app

关于python - lask.cli.NoAppException : While importing "app", 引发了 ImportError:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66716267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com