python flask ImportError: No module named views.login - flask

I have a app named flaskr. When I want to import views.login` from views. it gives me error:
File "C:\Projects\flaskr\flaskr.py", line 28, in <module>
from flaskr.views.login import login
ImportError: No module named views.login
Before I put all the methods inside the flaskr.py file. I want to move each functions to different views, like login, show_entries, log_out, just like MVC has different views.
flaskr.py
from sqlite3 import dbapi2 as sqlite3
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash
import os
app = Flask(__name__)
app.config.from_object(__name__)
from flaskr.views.login import login
app.register_blueprint(login)
if __name__ == '__main__':
app.run(debug = True)
login.py
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash
from jinja2 import TemplateNotFound
login = Blueprint('login', __name__,template_folder='../template')
#app.route('/login')
def login():
return render_template('login.html')

Create folder named app or whatever.
Create __init__.py file in app:
from sqlite3 import dbapi2 as sqlite3
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash
import os
app = Flask(__name__)
app.config.from_object(__name__)
from app import views
app.register_blueprint(views.login)
Move app.run method into separate start.py file in root directory
from app import app
if __name__ == '__main__':
app.run(debug = True)
Create folder views in app/. Create __init__.py in views
Now you could have structure like this:
your_project_name/
|app/
|__init__.py
|views/
|__init__.py
|login.py
start.py
Sorry, if there are have mistakes.

Related

ImportError: cannot import name 'app' from partially initialized module 'market' (most likely due to a circular import)-Flask

#market package
from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
from market import routes
app = Flask(__name__)
app.config\['SQLALCHEMY_DATABASE_URI'\] = 'sqlite:///market.db'
db = SQLAlchemy(app)
----------
from market import db #(models)
----------
from market import app #(routes)
from flask import render_template
from market.models import Item
----------#run
from market import app
if __name__ == '__main__':
app.run(debug=True)
Structuring the code into packages then importing modules but then getting the above error

flask NoAppException but flask run works

I have the following structure:
app_dir/
| myapi/
| __init__.py
| myapi_app.py
where myapi_app.py is
from myapi import create_app, db
app = create_app()
and myapi/__init__.py is
import logging
import os
from logging.handlers import RotatingFileHandler
from flask import Flask, request, current_app
from flask_sqlalchemy import SQLAlchemy
from myapi.config import Config
db = SQLAlchemy()
def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(config_class)
db.init_app(app)
...
return app
When I set FLASK_APP=myapi_app.py and run flask run from the app_dir directory, the flask service starts. However, when I make a request, I get the following error: flask.cli.NoAppException: Could not import "myapi_app". Where am I going wrong?
Your problem is that you are setting $FLASK_APP to the file in which the app variable is stored, you should instead set it to the python object path, e.g.
FLASK_APP=myapi_app:app
However, this is not necessary, as you could also just do:
FLASK_APP=myapi
as Flask will look for a create_app function in the package on its own.

In Flask is it possible to import views using the create_app() pattern without using Blueprints?

I have a very small Flask app that is laid out as follows:
tinker/
main.py
/my_package
init.py
views.py
When I do tinker>python main.py everything runs fine. Here are the contents of each file:
main.py:
from my_package import app
app.run()
my_package/init.py:
from flask import Flask
app = Flask(__name__)
from my_package import views
my_package/views.py:
from my_package import app
#app.route('/')
def home():
return 'Ola!!!!!'
While all the above code runs fine when I try to modify it slightly by using a a create_app() code pattern, as in the below, views.py throws the following exception: "ImportError: cannot import name 'app' from 'my_package' " Is there a way to fix the problem without using Blueprints?
main.py:
from my_package import create_app
app = create_app()
app.run()
my_package/init.py:
from flask import Flask
def create_app():
app = Flask(__name__)
from my_package import views
return app
my_package/views.py:
from my_package import app
#app.route('/')
def home():
return 'Ola!!!!!'
You import the views within application context, then in the views you can use current_app.
in mypackage/__init__.py:
def create_app():
app = Flask(__name__)
with app.app_context():
from . import views
in mypackage/views.py:
from flask import current_app
#current_app.route('/')
def index():
return 'hello, world!'
init.py needs to be renamed to __init__.py
Move app = Flask(__name__) outside of the create_app method
Change to from . import app in views.py

Flask - Attempted relative import in non-package

Following this tutorial on how to structure a Flask app, I have:
project/
__init__.py
app.py
models/
__init__.py
base.py
base.py
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
models/__init__.py
from .base import db
def init_app(app):
db.init_app(app)
project/__init__.py
from flask import Flask
def create_app()
from . import models, routes, services
app = Flask(__name__)
models.init_app(app)
# routes.init_app(app)
# services.init_app(app)
return app
finally, in app.py, I try to run it:
from . import create_app
app = create_app()
if __name__ == '__main__':
app.run(use_reloader=True, threaded=True, debug=True)
but I'm getting the error:
from . import create_app
ValueError: Attempted relative import in non-package
Am I building it right, what am I doing wrong?
I guess you are running your program by:
python project/app.py
In this case, you are not treat your "project" as a python package, which will raise the error you got. Instead, you can run your project with:
FLASK_APP=project.app flask run

how to create engine from flask-SQLAlchemy object?

I have the following code:
from flask import Flask, request, session, g, redirect, url_for, abort, \
render_template, flash
import pypyodbc
import pdb
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mssql+pypyodbc://SERVERNAME/DB'
db = SQLAlchemy(app)
#app.route('/')
def main():
result = db.engine.execute('SELECT * from Table')
return render_template('index.html')
This results in the following error:
ImportError: DLL load failed: The specified procedure could not be found.
I cannot find an example of this in the documentation...what is the correct way to use the SQLAlchemy object's engine?
Thanks!