#πŸ”’ attempted relative import with no known parent package

31 messages Β· Page 1 of 1 (latest)

celest ibex
#

Watching a tutorial and trying to import a database(db) from another python file. It was working fine but now I am getting this error, commenting out my own additions and still not working.

models.py
from . import db
from flask_login import UserMixin
from sqlalchemy.sql import func
from sqlalchemy.sql import expression

class Note(db.Model):
id = db.Column(db.Integer, primary_key=True)
data = db.Column(db.String(10000))
date = db.Column(db.DateTime(timezone=True), default=func.now())
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(150), unique=True)
password = db.Column(db.String(150))
first_name = db.Column(db.String(150))
notes = db.relationship('Note')

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from os import path
from flask_login import LoginManager

db = SQLAlchemy()
DB_NAME = "database.db"

init.py
def create_app():
app = Flask(name)
db_path = path.join(path.dirname(file), DB_NAME)
db_uri = 'sqlite:///{}'.format(db_path)
app.config['SECRET_KEY'] = 'hjshjhdjah kjshkjdhjs'
app.config['SQLALCHEMY_DATABASE_URI'] = db_uri
db.init_app(app)

from .views import views
from .auth import auth

app.register_blueprint(views, url_prefix='/')
app.register_blueprint(auth, url_prefix='/')

from .models import User, Note


login_manager = LoginManager()
login_manager.login_view = 'auth.login'
login_manager.init_app(app)

@login_manager.user_loader
def load_user(id):
    return models.User.query.get(int(id))



return app

def create_database(app):
if not path.exists('instance/' + DB_NAME):
with app.app_context():
db.create_all()
print("Database created!")

spare valveBOT
#

@celest ibex

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

wet trail
#

Relative imports are relative to the directory you are running in (current working directory).

pulsar mortar
#

That's not exactly true

celest ibex
#

but it didn't work

#

the initial stopped working suddenly and I really can't see why

pulsar mortar
#

no known parent package
This probably means you ran a file directly.

#

Try adding a empty file __init__.py in the directory

celest ibex
pulsar mortar
#

Run it with flask run

#

You need to run create_database first.

#

If you decorate create_database with @app.cli.command(), you can run it via flask create-database

celest ibex
#

would that not create any error

pulsar mortar
#

or do it in create_command via app.cli.add_command(create_database)

#

as far as I can tell, you aren't actually calling create_database

celest ibex
#

I'm confused because my database seems to be fine?

pulsar mortar
#

delete the database

#

let it recreate itself

#

foreign key check?

#

Isn't your error no such table: user

celest ibex
#

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: user
[SQL: SELECT user.id AS user_id, user.email AS user_email, user.password AS user_password, user.first_name AS user_first_name
FROM user
WHERE user.id = ?]
[parameters: (1,)]
(Background on this error at: https://sqlalche.me/e/20/e3q8)

#

I was getting this

#

but the deleting database worked @pulsar mortar thx

#

do I have to do this every time I run it then?

pulsar mortar
#

if you do, it means there's another issue corrupting your database

spare valveBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.