1

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()
choubix
  • 59
  • 1
  • 7
  • can you post the exact error? – Vivek Anand Sep 14 '15 at 05:26
  • have you tried the imports without the . in front of the files? – sihrc Sep 14 '15 at 05:28
  • possible duplicate of [Relative imports in Python 3](http://stackoverflow.com/questions/16981921/relative-imports-in-python-3) – ganduG Sep 14 '15 at 05:29
  • I also asked nearly the same question. However I have a different project layout: [How can I create imports that always work?](http://stackoverflow.com/questions/32553589/how-can-i-create-imports-that-always-work) – flammi88 Sep 14 '15 at 09:12

2 Answers2

1

Doing it your way, I get this ValueError:

Traceback (most recent call last):
   File "__init__.py", line 1, in <module>
       from .test import Chris

Taking out the . in front of the test works for me.

Alternatively

root   
pjct
├── pjctBB
    ├── __init__.py
    ├── models.py
    ├── views.py
    ├── pjctbb_sg.db 
    └── db_create.py
├── setup.py

Where setup.py:

from setuptools import setup, find_packages

if __name__ == "__main__":
    setup(
        name = "pjctBB",
        packages = find_packages(),
        version = "0.1.0"
    )

And python setup.py develop allows you to import via

from pjctBB.models import *
from pjctBB.views import *
sihrc
  • 2,728
  • 2
  • 22
  • 43
  • Hi all, thanks for helping me out. I tried taking the . out but it didnt work. I created "setup.py" file and amended the import as suggested and it works. I guess I have to go read about Setuptools asap! really grateful for that, thanks! Just a quick question: shouldnt I add the setup.py code into my "run.py to get it executed? (or call it from run.py?) thanks – choubix Sep 14 '15 at 10:06
  • You only need to run setup.py once. It's installing this project as a package imported by python wherever you are locally. the develop command basically means changes you make in the project will reflect the package installed. – sihrc Sep 14 '15 at 20:48
0

you forgot to insert the id column, in db_create.py

Mauro Rufino
  • 45
  • 1
  • 7