IntelliJ doesn't want to import - python

The problem is that IntelliJ Studio 13 doesn't want to recognize an import. I have my code like this
a folder named "app". Inside it
__init__.py
from flask import Flask
app = Flask(__name__)
from app import views, models
views.py
from flask import render_template, request
from models import *
from app import *
#app.route('/')
#app.route('/index')
def index():
return "123"
Now in views.py the "from app import *" is greyed as unused, and there's a red warning under #app with "unresolved reference "app".
Can anyone explain me why is this happening and what's the fix for this. Thanks in advance

from app import * will import the package contents into the current namespace. So the route function from the app package will be imported into the current namespace. Callable as route().
import app will import the app package into the current namespace as an object named app. Callable as app.route()
Generally the use of from app import * is frowned upon unless you are certain it is what you want to do. from app import route would be preferred.

Related

Cannot import database models in interactive terminal within Flask app context

I'm trying to test my database how it behaves in a many-to-many relationships. I'm struggling to access the application context of the Flask app.
I ran the Python terminal in the parent folder of the app and followed the steps found on SO and elsewhere. I could manage (i think) to import the app, create it, push the context, import the db. However, whenever I want to import models I'm prompted with a:
Error ModuleNotFoundError: No module named 'db'
Despite the fact that db is definately defined:
This is the screenshot of the complete process.
The folder structure is:
- app
--__int__.py
This is the app/init.py file:
from flask import Flask,Blueprint
from flask_sqlalchemy import SQLAlchemy
from app.config import Config
from mailjet_rest import Client
db = SQLAlchemy()
mailjet = Client(auth=(Config.MJ_APIKEY_PUBLIC, Config.MJ_APIKEY_PRIVATE), version='v3.1')
def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(Config)
db.init_app(app)
from app.views.users import users
from app.views.data import data
from app.views.admin import admin
app.register_blueprint(users,url_prefix='/users')
app.register_blueprint(data,url_prefix='/data')
app.register_blueprint(admin,url_prefix='/admin')
# with app.app_context():
# db.create_all()
return app
I'd greatly appreciate any input!
Thank you,
Matija
The solution was quite simple. I ought to import models from app.models and not from db.
The steps I took:
Rename the folder to app
Ran Flask shell command on the app level
Imported app
Import models from app.models

How do I import my error_handler module into my app.py in flask?

I separated my flask errorhandlers into a separate module called 'error_handlers.py' within my 'app.py' directory. I have tried importing the error_handlers module into the app module but i keep getting an ImportError:
from app import app
ImportError: cannot import name 'app' from partially initialized module 'app' (most likely due to a circular import)
I looked up a similar question and added blueprint but i'm still getting the same error. Please is there anything I'm doing wrong. My code below:
app.py
import os
from error_handler import *
from flask import Flask, flash, jsonify, redirect, render_template, request, session, abort
from flask_session import Session
from tempfile import mkdtemp
from helpers import lookup, login_required
# Configure application
app = Flask(__name__)
app.register_blueprint(error_handlers.blueprint)
...
error_handlers.py
from app import app
from flask import render_template, Blueprint
blueprint = Blueprint('error_handlers', __name__)
#blueprint.app_errorhandler(404)
def page_not_found(e):
return render_template("errors/404.html")
Hey you are getting Circular Import Error so change the name of your file or the function name must be different from the file name.
By this way, the compiler won't get confuse what the function to call.

Flask blueprints cannot import module

I'm learning Blueprints for Flask, but I am having trouble with importing the correct modules. This is my setup:
Folder structure:
- app.py
templates/
nomad/
- __init__.py
- nomad.py
app.py
from flask import Flask
from nomad.nomad import nblueprint
app = Flask(__name__)
app.register_blueprint(nblueprint)
nomad.py
from flask import render_template, Blueprint, abort
from app import app
nblueprint = Blueprint('nblueprint', __name__, template_folder='templates')
# Routes for this blueprint
#app.route ....
__init__.py is empty
The error I'm getting: ImportError: cannot import name nblueprint. I know my import statement is probably wrong, but what should it be and why?
EDIT:
If I remove from app import app, then I can successfully import nblueprint in app.py. But I need app in nomad.py because it needs to handle routes. Why is that line causing issues with importing, and how would I get around this?
Blueprints is for define application route so you don't need to use app instance and blueprint in same place for route defination.
#nomad.py
#nblueprint.route('/')
You are getting error because while you register the blueprint at the same time you use app instance. So as you said when you remove the from app ... it solve the problem.
The recommend way is define your view for that blueprint in blueprint package in your example nomad package, it should be like this:
...
nomad/
__init__.py
views.py
#nomad/__init__.py
nblueprint = Blueprint(...)
#nomad/views.py
from . import nblueprint
#nblueprint.route('/')
...

NameError when splitting app and views code

I want to divide my code into two parts, the app initialization and the view definitions. I import my views and define my app, but I get NameError: name 'manager' is not defined. Why am I getting this error? How do I split up my code correctly?
manage.py:
from flask import Flask,render_template
from flask.ext.script import Manager
import viewports
manager = Flask(__name__)
if __name__=='__main__':
manager.run()
viewports.py
#manager.route('/')
def Home():
return render_template('Home.html', title='FrontPage')
You created a circular import: first you import viewports, which imports manager, which is only defined after you import viewports. You also didn't organize your code correctly, everything should be under one package. You also confused creating a Flask-Script manager with creating a Flask app. Also, flask.ext is deprecated, import directly from the package name.
my_project/
my_app/
__init__.py
views.py
manage.py
__init__.py:
from flask import Flask
from flask_script import Manager
app = Flask(__name__)
manager = Manager(app)
from my_app import views
views.py:
from my_app import app
#app.route('/')
def index():
return 'Hello, World!'
manage.py:
from my_app import manager
manager.run()

Why doesn't this cause circular import

I have a flask app.
app.py
app = Flask(__name__)
from views import *
if __name__=="__main__":
app.run()
views.py
from app import app
#app.route('/')
def home():
return "Homepage"
So, here app.py is importing everything form views.py and views need app which is defined in app.py. But still its not causing circular import. Why?
I run this application using:
python app.py
This looks similar to the Larger Applications document which Flask allows you to do when creating apps.
From the docs:
Circular Imports
Every Python programmer hates them, and yet we just added some: circular imports (That’s when two modules depend on each other. In this case views.py depends on __init__.py). Be advised that this is a bad idea in general but here it is actually fine. The reason for this is that we are not actually using the views in __init__.py and just ensuring the module is imported and we are doing that at the bottom of the file.
If we try to follow what the program does, it is something like that:
app = Flask(__name__) # OK
from views import * # Goes into views.py
from app import app # Looks into app.py, finds it, import it
# Defines home
#app.route('/')
def home():
return "Homepage"
# import home and app, overriding app in app.py
# But views.app is the same as app.app, so it is still
# the same object
# Run main
if __name__=="__main__":
app.run()
I bet it computes something like that. Since app is defined before being imported, it's ok.

Categories