I am using Flask for my web framework. I am having an issue with imports. I am not understanding why can't I import my variable when I declare it within my my_app/__init__.py:
from flask import Flask
from flask_login import LoginManager
from my_app.some_module.my_class.py import auth
app = Flask(__name__)
login_manager = LoginManager()
class Config:
def __init__(self):
pass
config = Config()
My conflictuous imports are present in my_app/some_module/my_class.py:
from flask import Blueprint
from my_app import login_manager # this one works fine
from my_app import config
auth = Blueprint('auth', __name__)
I run the app with run.py:
from my_app import app
app.run(debug=True)
I then get the error:
Traceback (most recent call last):
...
File ".../my_app/some_module/my_class.py", line 1, in <module>
from my_app import login_manager, config
ImportError: cannot import name 'config' from 'my_app' (.../my_app/__init__.py)
Project structure is:
my_app
+ __init__.py
some_module
+ __init__.py
+ my_class.py
+ run.py
You have a cyclic import: my_app.some_module -> my_app.some_module.my_class -> my_app.some_module.
You can fix this by moving both Config and config to a separate module my_app.some_module.config.
# my_app.some_module.my_config
class Config:
pass
config = Config()
# my_app.some_module.my_class
from .my_config import config
# my_app.some_module.__init__
from .my_config import config
from .my_class import MyClass
This means that every import does not depend on previous imports:
my_app.some_module
|-> my_app.some_module.my_class -> my_app.some_module.config
\-> my_app.some_module.my_config
Doing imports this way instead of moving the import for .my_class to the end of __init__.py is more robust. You can freely reorder the imports of .my_class and .my_config at the top of files.
The problem is that you have a cyclic dependency. By the time you import auth from my_app.some_module.my_class.py your config is not set yet. Try moving that import to the end of the my_app/__init__.py file like:
from flask import Flask
from flask_login import LoginManager
app = Flask(__name__)
login_manager = LoginManager()
class Config:
pass
config = Config()
from my_app.some_module.my_class.py import auth
Related
I have a module called crop.py in my flask application. Everything works great until I want to import database parts from models.py. I'm guessing it has something to do with initializing the module into the app and then trying to import models into it, but I'm not sure.
Here is the error:
flask.cli.NoAppException: While importing 'test', an ImportError was raised.
Here is my basic app construction.
-app
models.py
crop.py
crop.py
from app import current_app
from flask import url_for, Markup
import PIL
from PIL import Image
from uuid import uuid4
import os
#Adding these below causes an importerror------------------------------------
#from app import db
#from app.models import Upload
class _Avatars(object):
#code here
class Avatars(object):
def __init__(self, app=None):
if app is not None:
self.init_app(app)
#code here
__init.py
from flask import Flask, request, current_app _l
from elasticsearch import Elasticsearch
from redis import Redis
import rq
from config import Config
#This is my module-----------------------------------------------------
from .crop import Avatars
db = SQLAlchemy()
migrate = Migrate()
avatars = Avatars()
def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(config_class)
db.init_app(app)
avatars.init_app(app)
I think it is causing a circular import error, since you're importing your crop model in your __init.py file and you're import db from __init.py into crop model file. Python does not allow this kind of scenario. Either you'll have to define your DB connections elsewhere in a separate file or add them manually everywhere you need to use the DB.
I'm trying to write some unit tests for a flask application I made. The project is setup like this:
apiproject (parent folder containing everything)
/venv
run.py
requirements.txt
/project
__init__.py
/departments
__init__.py
routes.py
models.py
/users
__init__.py
routes.py
models.py
/tests
TestUsers.py
run.py:
from project import app
if __name__ == '__main__':
app.run(debug=True)
my actual app is created under project/init.py
from flask import Flask, jsonify, Response
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
import os
from dotenv import load_dotenv
from flask_cors import CORS
load_dotenv(verbose=False)
DB_URL = os.getenv("DATABASE_URL")
# initialize the application
app = Flask(__name__)
and in my TestUsers.py I have this:
import json
from project import app
import unittest
from dotenv import load_dotenv
load_env(verbose=False)
class TestUsers(unittest.TestCase):
def setUp(self):
self.app = app
self.url_prefix = prefix = os.getenv("URL_PREFIX")
self.client = self.app.test_client()
def test_index(self):
response = self.client.get(self.url_prefix + '/')
self.assertEqual(response.status_code, 200)
if __name__ == '__main__':
unittest.main()
When I run TestUsers.py, I get ModuleNotFoundError: No module named 'project'. I tried doing sys.path.append('../') and ../../ inside of TestUsers.py, but that didn't help.
inside TestUsers.py, before importing app:
https://stackoverflow.com/a/22737042/7858114
I think you need an init.py in your apiproject folder too.
I am learning flask. I have a directory structure that looks something like this.
project
- controllers
-- auth.py
-- main.py
- db
-- setup.py
- models
-- models.py
- templates
-- base.html
-- index.html
-- login.html
-- signup.html
-- 404.html
-__init__.py
The file init.py looks like this
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'] = '9OLWxND4o83j4K4iuopO'
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:#localhost/mission_to_mars'
db.init_app(app)
# blueprint for auth routes in our app
from .controllers.auth import auth as auth_blueprint
app.register_blueprint(auth_blueprint)
# blueprint for non-auth parts of app
from .controllers.main import main as main_blueprint
app.register_blueprint(main_blueprint)
return app
I'm trying to import the db variable and create_app() function in controllers/auth.py, controllers/main.py, models/models.py, db/setup.py
I have tried the below syntaxes
from .. import db, create_app
which gives the error:
ImportError: attempted relative import with no known parent package
I have also tried
from project import db, create_app
which gives the error
ModuleNotFoundError: No module named 'project'
I want to know how I can import classes from different directories so that I can use them further in the project.
Thanks in advance
I'm trying to passing the flask app context with some global variable to the nested api context.
run.py <= Start the flask application via app.run()
app/
__init__.py
api/
__init__.py
controllers.py
app/__init__.py
from flask import Flask
from app.api.controllers import api
app = Flask(__name__)
app.register_blueprint(api, url_prefix='/api')
main_dict = {"a":"info1", "b":"info2}
app/api/controllers.py
from flask import Blueprint, jsonify
from app import app, main_dict
api = Blueprint('api', __name__)
#api.route('/')
def test():
return jsonify({"status":"OK"})
It throws the following error message:
ImportError: cannot import name 'app'
Thanks!
You've created a circular dependency.
app wants to import app.api.controllers, but that wants to import from app. What you should do to fix it, is to create a new module containing main_dict and import that in both app and app.api.controllers:
# app/__init__.py
from .main_dict import main_dict
from .api.controllers import api
# app/main_dict.py
main_dict = {'a': 'A'}
# app/api/__init__.py
# app/api/controllers.py
from flask import current_app as app
from ..main_dict import main_dict
if you need access to the app outside of app/__init__.py, the recommended way to get at it is to use from flask import current_app
This is how my folder and files looks like:
/app
__init__.py
/admin
__init__.py
models.py
and app/__init__.py file:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from app.admin.models import *
app = Flask('app')
#app.route('/users', methods=['GET'])
def show_users():
return "list of all Users:\n" + User.query.limit(5).all()
and app/admin/models.py :
from datetime import datetime
from sqlalchemy.orm import relationship
from flask_sqlalchemy import SQLAlchemy
from app import app
db = SQLAlchemy(app)
class User(db.Model):
pass
I want have access to my User model in init file,from Import from parent directory and import script from a parenet directory I have tried from app import * or from .. import app or also put db = SQLAlchemy(app) in __init__ file and import it in models.py with from app import db or from .. import db but I'm keep getting app and db are not defined and also I think it's weird that I should import both file in each other.
The error is about Flask extensions, while your app created sqlalchemy extension didn't initialize, therefore it cause error. The best practice is to keep extensions initializes in same place:
#app/__init__.py
...
db = SQLAlchemy(app)
from app.admin.models import *
#app/admin/models.py
...
from app import db
If you do this changes, it will work.