Ambasador
Ambasador

Reputation: 377

Failed to find Flask application or factory in module "config" Use "FLASK_APP=config:name to specify one

I am doing the flask training course enter link description here

I'm install Flask-SQLAlchemy in a virtual environment. I'm install Flask-Migrate: pip install flask-migrate

Created a class config.py:

import os
basedir = os.path.abspath(os.path.dirname(__file__))

class Config(object):
    SECRET_KEY = os.environ.get('SECRET_KEY') or '*********'    
    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or 'sqlite:///' + os.path.join(basedir, 'app.db')  
    SQLALCHEMY_TRACK_MODIFICATIONS = False

In models.py I have two models:

from datetime import datetime
from app import db

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), index=True, unique=True)
    email = db.Column(db.String(120), index=True, unique=True)
    password_hash = db.Column(db.String(128))
    posts = db.relationship('Post', backref='author', lazy='dynamic')

    def __repr__(self):
        return '<User {}>'.format(self.username)

class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    body = db.Column(db.String(140))
    timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

    def __repr__(self):
        return '<Post {}>'.format(self.body)

In testapp.py only:

from app import app

In file init.py

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from config import Config

app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)

from app import routes, models

Structure my app:

testapp\
  venv\
  app\
    templates\
        base.html
        index.html
        login.html 
    __init__.py
    routes.py
    forms.py
    models.py    
  testapp.py
  config.py

In a Python interpreter session, do the following from app.models import User and get an error:

(venv) C:\Users\User\testapp>python
Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 
64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> from app.models import User
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "C:\Users\User\testapp\app\__init__.py", line 4, in <module>
     from config import Config
   File "C:\Users\User\testapp\config.py", line 8
     SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \
                                                                 ^
TabError: inconsistent use of tabs and spaces in indentation
 >>>

Set the environment variable for the new config module set FLASK_APP=config.py

When I execute the command:

flask db init

Complete error log:

(venv) C:\Users\User\testapp>flask db init
Usage: flask db init [OPTIONS]

Error: Failed to find Flask application or factory in module "config". Use "FLAS
K_APP=config:name to specify one.

(venv) C:\Users\User\testapp>

What am I doing wrong? Help

Upvotes: 1

Views: 9393

Answers (1)

Run this command in cmd

export FLASK_APP=testapp.py

before

flask db init

Upvotes: 2

Related Questions