I started building my flask app with a very basic structure detailed here, basically everything in one directory. As I have added more dependencies to my project, the need to separate my app into individual components has come up. Here is a look at how I have everything organized right now:
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
My problem is that after running the basic commands export FLASK_APP=run.py
& flask run
I get the following error:
Error: While importing 'run', an ImportError was raised.
I can't figure out what is causing this error. Any insight would be greatly appreciated. Here are the contents of run.py:
from App import create_app
app = create_app()
if __name__ == "__main__":
app.run(debug=True)
Contents of App/ 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
Contents of 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.
Contents of 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'
Again, I really appreciate anyone who takes the time to help.
Big thanks to @furas. My issue was not in any of the files in the original question. I had to run python run.py
in order to get a more in depth logging response that included a Traceback report. The Traceback I received uncovered a Circular Import I had within my application after restructuring.
At times simply running flask run
would provide a Traceback to the problem, but Im guessing that when the issue is with the initialization of the app flask run
is not enough