I am fairly new to programming. I am using Pycharm IDE and the flask framework. I am now trying to use SQLAlchemy and when I try to run a query (db_create.py) to add stuff in the DB it returns "SystemError: Parent module '' not loaded, cannot perform relative import".
From what I gathered on the internet, it seems to be a Pycharm issue but I dont really understand how to troubleshoot it.
Does anyone have an idea please?
For reference, my project structure is as follows (pjctBB should be tabulated as it it part of pjctMain).
I really appreciate your time in reading this and helpign me out. Thanks in advance!
pjctMain
├── run.py
pjctBB
├── __init__.py
├── models.py
├── views.py
├── pjctbb_sg.db
└── db_create.py
My run.py:
from pjctBB import app
import os
app.secret_key = os.urandom(24)
app.run(debug=True)
My init:
from .views import app
from .models import *
My models.py contains this:
from .views import db
class User(db.Model):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String, nullable=False)
username = db.Column(db.String, nullable=False)
password = db.Column(db.String, nullable=False)
rank = db.Column(db.Integer, nullable=False)
def __init__(self, email, username, password, rank):
self.email = email
self.username = username
self.password = password
self.rank = rank
My (partial) views.py has the following:
from flask import Flask, \
render_template, request, redirect, url_for, session, flash, json
# from .models import register1
import os
from flask_sqlalchemy import SQLAlchemy
#import sqlite3
#from passlib.hash import bcrypt
from functools import wraps
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///pjctbb_sg.db'
# Create SQLAlchemy object
db = SQLAlchemy(app)
Finally, my db_create.py:
from .views import db
from .models import User
db.create_all()
db.session.add(User("test@test.com", "test", "000000", "50"))
db.session.commit()