我正在使用flask_restful
创建一个个人应用程序,我正在使用单个models.py
和app.py
执行所有操作,但应用程序将会增长,所以我决定制作一些文件夹重组。
我目前有以下结构:
/project_folder
application_name
__init__.py
controllers.py
models.py
config.py
manage.py
run.py
tests
__init__.py
test_controller.py
到目前为止一切都有效,但我希望结构如下:
/project_folder
application_name
__init__.py
controllers
__init__.py
brewery_controller.py
...others
models
__init__.py
base_model.py
brewery.py
...others
config.py
manage.py
run.py
tests
__init__.py
test_controller.py
但我似乎无法使其发挥作用。这是应用程序__init__.py
#/project_folder/application_name/__init__.py
import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_restful import Api
from controllers import BreweryList, BreweryResource
from models import db
def initApp(config):
app = Flask(__name__)
app.config.from_object(config)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)
api = Api(app)
api.add_resource(BreweryList, '/breweries')
api.add_resource(BreweryResource, '/breweries/<brewery_id>')
return app
我试过
from brewery_controller import BreweryList, BreweryResource
from base_model import db
没有运气。如果我取消注释Brewery类的导入行,我会继续获得ImportError: cannot import BreweryList
db
同样如此。
controllers/__init__.py
和models/__init__.py
都是空的。
这里也是run.py
import os
from beerinv import initApp
if __name__ == '__main__':
app = initApp(os.environ['APP_SETTINGS'])
app.run()
答案 0 :(得分:0)
可以通过输入完整的导入路径来关注@davidism注释来解决问题:
from application_name.controllers.brewery_controller import BreweryList, BreweryResource
from application_name.models.base_model import db