0

I am getting import error from general BluePrint, I am unable to figure out why is this happening when
I am using same code in my other auth BluePrint
Project Tree:

├── app.py
├── auth
│   ├── auth.py
│   └──  __init__.py         
├── config.py
├── general
│   ├── general.py
│   └──  __init__.py
├── __init__.py
└──  models.py
  • All init.py files are empty

Inside auth.py:

import sys
sys.path.insert(0,'..')

from flask import ( 
    Blueprint, 
    render_template, 
    request, 
    jsonify, 
    make_response 
    )

auth_bp = Blueprint('auth_bp', 
    __name__)

from CommunityAttendanceApp.models import Users
from CommunityAttendanceApp.app import db

@auth_bp.route('/user/registration', methods=['POST'])
def user_registration():
    """ Code """

Inside general.py:

import sys
sys.path.insert(0,'..')

from flask import ( 
    Blueprint, 
    render_template, 
    request, 
    jsonify, 
    make_response 
    )

general_bp = Blueprint('general_bp', 
    __name__)

from CommunityAttendanceApp.models import Users
from CommunityAttendanceApp.app import db

# HomePage for Everyone
@general_bp.route('/', methods=['GET'])
def testing_route():
    """ code """

Inside models.py:

from app import db


class Users(db.Model):

    __tablename__ = "users"

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(40), unique=True, nullable=False)
    password = db.Column(db.String(60), nullable=False)

    def __repr__(self):
        return f"User('{self.username}', '{self.email}')"

Inside app.py:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from os import getcwd, environ
from os.path import join

app = Flask(__name__)
app.config['DEBUG']=True #--todo--

PATH_TO_CONFIG = join(getcwd(), 'config.py')
app.config.from_pyfile(PATH_TO_CONFIG)

app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

PG = environ.get("DATABASE_URL")
PATH_TO_LOCAL_DB = join(getcwd(), 'db.sqlite')
if PG is None or PG=="":
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + \
        PATH_TO_LOCAL_DB
else:
    app.config['SQLALCHEMY_DATABASE_URI'] = PG

db = SQLAlchemy(app)


# Importing BluePrint
from auth.auth import auth_bp
from general.general import general_bp

# Registring BluePrint
app.register_blueprint(auth_bp, url_prefix='/auth')
app.register_blueprint(general_bp, url_prefix='')

if __name__ == "__main__":
    app.run()

Error Trace-back below:

Traceback (most recent call last):
  File "app.py", line 26, in <module>
    from auth.auth import auth_bp
  File "/root/Desktop/GitHub/Hosted/CommunityAttendanceApp/auth/auth.py", line 15, in <module>
    from CommunityAttendanceApp.models import Users
  File "../CommunityAttendanceApp/models.py", line 1, in <module>
    from app import db
  File "/root/Desktop/GitHub/Hosted/CommunityAttendanceApp/app.py", line 27, in <module>
    from general.general import general_bp
  File "/root/Desktop/GitHub/Hosted/CommunityAttendanceApp/general/general.py", line 15, in <module>
    from CommunityAttendanceApp.models import Users
ImportError: cannot import name 'Users' from 'CommunityAttendanceApp.models' (../CommunityAttendanceApp/models.py)

Command I used is python3 app.py
I would like to tell that using flask run gives different error, which is Import Error (I am not using this when deploying, sharing if this can bring some light)

 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
Usage: flask run [OPTIONS]

Error: While importing "CommunityAttendanceApp.app", an ImportError was raised:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/flask/cli.py", line 240, in locate_app
    __import__(module_name)
  File "/root/Desktop/GitHub/Hosted/CommunityAttendanceApp/app.py", line 26, in <module>
    from auth.auth import auth_bp
ModuleNotFoundError: No module named 'auth'

0 Answers0