I'm having difficulty getting my flask app to run by using the "python" method. I have no problems using
export FLASK_APP=microblog.py
flask run
but attempting to use
python microblog.py
will result in the following error
ImportError: No module named 'app'
where my microblog.py file looks like this:
from app import app, db
from app.models import User
@app.shell_context_processor
def make_shell_context():
return {'db': db, 'User': User}
and my __init__.py
file looks like this:
from flask import Flask
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager
app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
login = LoginManager(app)
login.login_view = 'login'
from app import routes
if __name__ == '__main__':
app.run()
The folder these files reside in is called 'app'. I've tried putting the
if __name__ == '__main__':
app.run()
In the microblog.py file as well, which was also unsuccessful.
I also have folders for templates/static/etc. and a routes file where I run all my @app.route()
commands
Again, everything works fine when I run "flask run" in terminal, it just breaks down when I try using "python microblog.py". I'm trying to launch the app on AWS and just about every tutorial requires the applications to be called using the "python" method. Any help is appreciated.
[Solved]
I had my directories mixed up. I pulled microblog.py
back outside of the app
folder and was able to run python microblog.py
with no issues and without having to edit anything.
In hindsight, I probably should have posted the file structure of everything right in the beginning.
In this line from app import app, db
you're trying to import a module called app.app
, but it apparently does not exists in your project source.
Try to create a file named app.py
inside app/
path, copy and paste all content of __init__.py
file inside of it.
Your __init__.py
must be blank. It's just indicate that your path app/
is a importable module.
See this article.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With