Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flask run vs. python

Tags:

python

flask

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.

like image 843
aalberti333 Avatar asked Mar 12 '18 15:03

aalberti333


2 Answers

[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.

like image 56
aalberti333 Avatar answered Oct 03 '22 05:10

aalberti333


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.

like image 38
Abe Avatar answered Oct 03 '22 04:10

Abe