user11946462
user11946462

Reputation:

Importing from __init__ in flask project same directory

init.py and models.py are in the same directory (myproject)

myproject
 |
 -- __init__.py
 -- models.py
 |
 app.py

When I try to import something inside models.py from init I get:

ModuleNotFoundError: No module named 'myproject'

What I try:

from myproject import db,login_manager

How can I import something from init (e.g. the database) when it is inside the same directory as the other .py file?

Inside init:

import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager


login_manager = LoginManager()

app = Flask(__name__)

app.config['SECRET_KEY'] = 'mysecretkey'
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + 
os.path.join(basedir, 'data.sqlite')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)
Migrate(app,db)

login_manager.init_app(app)

login_manager.login_view = "login"

Inside models.py:

from myproject import db,login_manager
from werkzeug.security import 
generate_password_hash,check_password_hash
from flask_login import UserMixin

@login_manager.user_loader
def load_user(user_id):
    return User.query.get(user_id)

class User(db.Model, UserMixin):


    __tablename__ = 'users'


    id = db.Column(db.Integer, primary_key = True)
    email = db.Column(db.String(64), unique=True, index=True)
    username = db.Column(db.String(64), unique=True, index=True)
    password_hash = db.Column(db.String(128))

    def __init__(self, email, username, password):
        self.email = email
        self.username = username
        self.password_hash = generate_password_hash(password)

    def check_password(self,password):

        return check_password_hash(self.password_hash,password)

Inside app.py (where I am exectuing my project from - it also works) just wondering why the import in models.py outputs the error message

from myproject import app,db
from flask import render_template, redirect, request, url_for, 
flash,abort
from flask_login import login_user,login_required,logout_user
from myproject.models import User
from myproject.forms import LoginForm, RegistrationForm
from werkzeug.security import generate_password_hash, 
check_password_hash


@app.route('/')
def home():
    return render_template('home.html')

-- (some more view functions) --





if __name__ == '__main__':
    app.run(debug=True)

Upvotes: 0

Views: 1277

Answers (2)

Sujan Adiga
Sujan Adiga

Reputation: 1371

You need to set the PYTHONPATH such that model.py can find myproject module.

If you're running flask shell, try the following inside myproject directory

export PYTHONPATH=.
flask shell

Ref: https://docs.python.org/2/using/cmdline.html#envvar-PYTHONPATH

Upvotes: 0

noslenkwah
noslenkwah

Reputation: 1744

Use the . to import from the same level.

from . import db, login_manager

Upvotes: 1

Related Questions