unnk
unnk

Reputation: 21

Set FLASK_APP after restructuring Flask into package

I cannot set the FLASK_APP variable after restructuring it into a package, it gives an error Could not locate a Flask application. Use the 'flask --app' option, 'FLASK_APP' environment variable, or a 'wsgi.py' or 'app.py' file in the current directory.

Below is the file organization,

File organization

And the contents of the __init__.py are as follows which is in the package flaskblog as shown above.

from flask import Flask
from flask_sqlalchemy import SQLAlchemy


app= Flask(__name__)
app.config['SECRET_KEY']= ''
app.config['SQLALCHEMY_DATABASE_URI']='sqlite:///site.db'
db= SQLAlchemy(app)

from flaskblog import routes

And the contents of the run.py are as follows

from flaskblog import app

if __name__ =='__main__':
    app.run(debug= True)

I want to set the FLASK_APP environment variable so that I can activate the SQLite database using flask shell. However while setting the (.venv) PS C:\Users\sample projects\flask_blog> set FLASK_APP=flaskblog

I get

Usage: flask shell [OPTIONS]
Try 'flask shell --help' for help.

Error: Could not locate a Flask application. Use the 'flask --app' option, 'FLASK_APP' environment variable, or a 'wsgi.py' or 'app.py' file in the current directory.

But the python run.py gets executed.

Upvotes: 1

Views: 65

Answers (2)

unnk
unnk

Reputation: 21

This problem was solved by using creating a separate file .flaskenv in the project directory and then setting the FLASK_APP environment variable inside the the file.

In '.flaskenv'

FLASK_APP=flaskblog

Then installing pip install python-dotenv

Then running flask shell worked!

Upvotes: 1

Carmelot
Carmelot

Reputation: 112

You can try to change your env variables like

bash

set FLASK_APP=flaskblog

powershell

$env:FLASK_APP = "flaskblog"

And also i wouldnt call __run__.py, i would call simply run.py

Upvotes: 0

Related Questions