重新组织项目目录后无法将 Flask App 升级到 运行
Can't get Flask App to run after Re-Organizing Project Directory
我开始构建我的 Flask 应用程序时使用了非常基本的详细结构 here,基本上所有内容都在一个目录中。当我向我的项目添加更多依赖项时,需要将我的应用程序分成单独的组件。看看我现在是如何组织一切的:
Real-Life-Application
│
├── App
│ ├── UI
│ │ ├── static
│ │ │ ├── css
│ │ │ │ └── main.css
│ │ │ ├── images
│ │ │ │ ├── album_placeholder.jpeg
│ │ │ │ ├── header.jpeg
│ │ │ │ └── logo.png
│ │ │ └── javascript
│ │ │ ├── create.js
│ │ │ └── tracks.js
│ │ └── templates
│ │ ├── base.html
│ │ ├── create.html
│ │ ├── index.html
│ │ ├── information.html
│ │ └── tracks.html
│ ├── __init__.py
│ ├── authenticate.py
│ ├── db_actions.py
│ ├── routes.py
│ ├── services.py
│ └── user_operations.py
├── Pipfile
├── Pipfile.lock
├── Procfile
├── README.md
├── Testing
│ ├── __init__.py
│ └── testing.py
├── __pycache__
│ ├── authenticate.cpython-39.pyc
│ ├── config.cpython-39.pyc
│ ├── main.cpython-39.pyc
│ ├── services.cpython-39.pyc
│ └── user_operations.cpython-39.pyc
├── config.py
├── requirements.txt
├── run.py
└── setup.py
我的问题是,在 运行 基本命令 export FLASK_APP=run.py
& flask run
之后,我收到以下错误:
Error: While importing 'run', an ImportError was raised.
我不知道是什么导致了这个错误。任何见解将不胜感激。
以下是run.py的内容:
from App import create_app
app = create_app()
if __name__ == "__main__":
app.run(debug=True)
应用程序的内容/ init.py:
from flask import Flask
import config
from flask_bootstrap import Bootstrap
from apscheduler.schedulers.background import BackgroundScheduler
import sqlalchemy
def create_app() -> Flask:
app = Flask(__name__)
app.config.from_object(config)
app.secret_key = app.config['CLIENT_SECRET']
# connect to local instance of mysqlDB server
engine = sqlalchemy.create_engine('sqlite:///:memory:', echo=True)
# create session and base declarative
from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind=engine)
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
# make sure user table is created
from App.user_operations import User
Base.metadata.create_all(engine)
# schedule updates for the TopTracks playlists
from App.services import updatePlaylists
scheduler = BackgroundScheduler()
scheduler.add_job(updatePlaylists, trigger='interval', days=1)
scheduler.start()
import App.routes
bootstrap = Bootstrap(app)
return app
App/routes.py 的内容:
import logging
import time
# Flask Imports
from flask import (jsonify, make_response, redirect, render_template,
request, session)
# Local Imports
from authenticate import createStateKey, getToken
from App import app
from services import (addTracksPlaylist, createPlaylist, getAllTopTracks,
getRecommendedTracks, getTopTracksURI,searchSpotify,
createRadarChart, getLikedTrackIds, likedTrackIdsDataFrame,
normalizeDf)
from user_operations import (addUser, getUserInformation)
@app.route('/')
@app.route('/index')
def index():
"""
Homepage.
"""
return render_template('index.html')
# More route definitions come after this of course.
config.py
的内容
CLIENT_ID = ''
CLIENT_SECRET = ''
REDIRECT_URI = 'http://127.0.0.1:5000/callback'
RESPONSE_TYPE = 'code'
GRANT_TYPE = 'authorization_code'
TOKEN_URL = ''
SCOPE = 'user-read-email'
AUTHORIZATION = 'Authorization: Basic *<base64 encoded client_id:client_secret>*'
DATABASE_PASSWORD = ''
DATABASE_NAME = 'DB'
再次感谢所有愿意花时间提供帮助的人。
非常感谢@furas。我的问题不在原始问题的任何文件中。我必须 运行 python run.py
才能获得更深入的日志记录响应,其中包括回溯报告。我收到的 Traceback 发现我在重组后的应用程序中有一个 Circular Import。
有时 运行ning flask run
会提供对问题的追溯,但我猜测当问题出在应用程序初始化时 flask run
是不够的
我开始构建我的 Flask 应用程序时使用了非常基本的详细结构 here,基本上所有内容都在一个目录中。当我向我的项目添加更多依赖项时,需要将我的应用程序分成单独的组件。看看我现在是如何组织一切的:
Real-Life-Application
│
├── App
│ ├── UI
│ │ ├── static
│ │ │ ├── css
│ │ │ │ └── main.css
│ │ │ ├── images
│ │ │ │ ├── album_placeholder.jpeg
│ │ │ │ ├── header.jpeg
│ │ │ │ └── logo.png
│ │ │ └── javascript
│ │ │ ├── create.js
│ │ │ └── tracks.js
│ │ └── templates
│ │ ├── base.html
│ │ ├── create.html
│ │ ├── index.html
│ │ ├── information.html
│ │ └── tracks.html
│ ├── __init__.py
│ ├── authenticate.py
│ ├── db_actions.py
│ ├── routes.py
│ ├── services.py
│ └── user_operations.py
├── Pipfile
├── Pipfile.lock
├── Procfile
├── README.md
├── Testing
│ ├── __init__.py
│ └── testing.py
├── __pycache__
│ ├── authenticate.cpython-39.pyc
│ ├── config.cpython-39.pyc
│ ├── main.cpython-39.pyc
│ ├── services.cpython-39.pyc
│ └── user_operations.cpython-39.pyc
├── config.py
├── requirements.txt
├── run.py
└── setup.py
我的问题是,在 运行 基本命令 export FLASK_APP=run.py
& flask run
之后,我收到以下错误:
Error: While importing 'run', an ImportError was raised.
我不知道是什么导致了这个错误。任何见解将不胜感激。 以下是run.py的内容:
from App import create_app
app = create_app()
if __name__ == "__main__":
app.run(debug=True)
应用程序的内容/ init.py:
from flask import Flask
import config
from flask_bootstrap import Bootstrap
from apscheduler.schedulers.background import BackgroundScheduler
import sqlalchemy
def create_app() -> Flask:
app = Flask(__name__)
app.config.from_object(config)
app.secret_key = app.config['CLIENT_SECRET']
# connect to local instance of mysqlDB server
engine = sqlalchemy.create_engine('sqlite:///:memory:', echo=True)
# create session and base declarative
from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind=engine)
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
# make sure user table is created
from App.user_operations import User
Base.metadata.create_all(engine)
# schedule updates for the TopTracks playlists
from App.services import updatePlaylists
scheduler = BackgroundScheduler()
scheduler.add_job(updatePlaylists, trigger='interval', days=1)
scheduler.start()
import App.routes
bootstrap = Bootstrap(app)
return app
App/routes.py 的内容:
import logging
import time
# Flask Imports
from flask import (jsonify, make_response, redirect, render_template,
request, session)
# Local Imports
from authenticate import createStateKey, getToken
from App import app
from services import (addTracksPlaylist, createPlaylist, getAllTopTracks,
getRecommendedTracks, getTopTracksURI,searchSpotify,
createRadarChart, getLikedTrackIds, likedTrackIdsDataFrame,
normalizeDf)
from user_operations import (addUser, getUserInformation)
@app.route('/')
@app.route('/index')
def index():
"""
Homepage.
"""
return render_template('index.html')
# More route definitions come after this of course.
config.py
的内容CLIENT_ID = ''
CLIENT_SECRET = ''
REDIRECT_URI = 'http://127.0.0.1:5000/callback'
RESPONSE_TYPE = 'code'
GRANT_TYPE = 'authorization_code'
TOKEN_URL = ''
SCOPE = 'user-read-email'
AUTHORIZATION = 'Authorization: Basic *<base64 encoded client_id:client_secret>*'
DATABASE_PASSWORD = ''
DATABASE_NAME = 'DB'
再次感谢所有愿意花时间提供帮助的人。
非常感谢@furas。我的问题不在原始问题的任何文件中。我必须 运行 python run.py
才能获得更深入的日志记录响应,其中包括回溯报告。我收到的 Traceback 发现我在重组后的应用程序中有一个 Circular Import。
有时 运行ning flask run
会提供对问题的追溯,但我猜测当问题出在应用程序初始化时 flask run
是不够的