-1

I try to implement an authentification app with python, SQLAlchemy and Flask. I found https://www.digitalocean.com/community/tutorials/how-to-add-authentication-to-your-app-with-flask-login-fr. But after I defined set FLASK_APP=project and set FLASK_DEBUG=1, i tried to run my app with flask run, i get the error :

Error: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not 
found in the current directory.

app.py :

from flask import Blueprint

main = Blueprint('main', __name__)

@main.route('/')
def index():
    return 'Index'

@main.route('/profile')
def profile():
    return 'Profile' 

auth.py :

from flask import Blueprint
#from . import db

auth = Blueprint('auth', __name__)

@auth.route('/login')
def login():
    return 'Login'

@auth.route('/signup')
def signup():
    return 'Signup'

@auth.route('/logout')
def logout():
    return 'Logout'

init.py :

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

# init SQLAlchemy so we can use it later in our models
db = SQLAlchemy()

def create_app():
    app = Flask(__name__)

    app.config['SECRET_KEY'] = 'secret-key-goes-here'
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'

    db.init_app(app)

    # blueprint for auth routes in our app
    from auth import auth as auth_blueprint
    app.register_blueprint(auth_blueprint)

    # blueprint for non-auth parts of app
    from app import main as main_blueprint
    app.register_blueprint(main_blueprint)

    return app

i exactly following the tuto and i use a venv Have you an idea ?

1 Answers1

0

You can follow these steps for installation (Linux):

create a new folder and goyour folder path :

$ mkdir myproject 
$ cd myproject

install virtualenvironment and create a new virtualenvironment named venv :

$ pip install virtualenv
$ virtualenv venv

activate your venv :

$ source venv/bin/activate

And now install Flask!

$ pip install Flask

If you done, create a new python file named proj.py in your directory: proj.py

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"

If you use an editor like pycharm, open editor's terminal or your local terminal and write code below:

$ export FLASK_APP=proj
$ export FLASK_ENV=development
$ flask run

or

$ flask run --host=0.0.0.0

And in debug mode you can code like this:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"

if __name__ == '__main__':
   app.run(host='0.0.0.0',port=8000,debug=True)

and run :

python proj.py
Ayse
  • 576
  • 4
  • 13
  • in digitalocean, they don't use "if __name__ == '__main__': [...]" like i ever do i saw some things about "flask run" and i try a minimal app, it's working. I think come from blueprint thanks a lot for your answer ! ^^ – POIROT Cloé Jun 23 '21 at 10:02