1

Noob level here. It seems my models.py file can't import the db object created in init.py, but a db file (blog.db) is still being created. Since I can't use my model class (Entry(db.Model)) to add to the database from within the console (the db object itself works in the console), I think I'm importing incorrectly from within the models.py. On Python3 (venv), Linux.

Dir structure (relevant folders only, ex. no cache):

/folder/
/folder/app/
/folder/app/__init__.py
/folder/app/models.py
/folder/config.py (only includes the sqlite uri naming variable)
/folder/fakepy/etc (venv, with flask and flask_sqlalchemy)
/folder/run.py (starts server fine, but does show that 'common' sql track modifications warning twice after a restart. normal?)

__init__.py:

#!fakepy/bin/python3 (error happens with or without shebang(s))
from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)

from app import models

models.py:

#!fakepy/bin/python3
from app import db
import re
import datetime

class Entry(db.Model):
    __tablename__ = "post"
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(80))
    slug = db.Column(db.String, unique=True)
    content = db.Column(db.Text)
    timestamp = db.Column(db.DateTime)
    def __init__(self, title, content, slug=None, timestamp=None):
        self.title = title
        self.content = content
        if slug is None:
            self.slug = re.sub('[^\w]+', '-', self.title.lower())
        if timestamp is None:
            timestamp = datetime.utcnow()
        self.timestamp = timestamp

config.py:

import os

basedir = os.path.abspath(os.path.dirname(__file__))

SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'blog.db')

Database is created... (console opened at /folder/, blog.db created at /folder/):

>>> from app import db
>>> db.create_all()
>>>

ERROR: name 'Entry' is not defined when app is imported from console

(fakepy) cha0@skyrim:~/folder$ python3
Python 3.4.3+ (etc removed for brevity)
>>> from app import app
(common sql track modifications warning, removed it for brevity)
>>> Entry()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'Entry' is not defined
>>> 
>>> from app import models
>>> Entry()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'Entry' is not defined
 >>>
 >>> from app import Entry
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'Entry'
>>> 

ERROR: No module named 'app' when models.py is ran directly.

(fakepy) cha0@skyrim:~/folder/app$ python3 models.py 
Traceback (most recent call last):
  File "models.py", line 2, in <module>
    from app import db
ImportError: No module named 'app'
(fakepy)

ERROR: No module named 'config' when init.py is ran directly. I think this may be a separate issue though.

python3 __init__.py
Traceback (most recent call last):
  File "/home/cha0/folder/fakepy/lib/python3.4/site-packages/werkzeug/utils.py", line 418, in import_string
    __import__(import_name)
ImportError: No module named 'config'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "__init__.py", line 6, in <module>
    app.config.from_object('config')
  File "/home/cha0/folder/fakepy/lib/python3.4/site-packages/flask/config.py", line 162, in from_object
    obj = import_string(obj)
  File "/home/cha0/folder/fakepy/lib/python3.4/site-packages/werkzeug/utils.py", line 443, in import_string
    sys.exc_info()[2])
  File "/home/cha0/folder/fakepy/lib/python3.4/site-packages/werkzeug/_compat.py", line 137, in reraise
    raise value.with_traceback(tb)
  File "/home/cha0/folder/fakepy/lib/python3.4/site-packages/werkzeug/utils.py", line 418, in import_string
    __import__(import_name)
werkzeug.utils.ImportStringError: import_string() failed for 'config'. Possible reasons are:

- missing __init__.py in a package;
- package or module path not included in sys.path;
- duplicated package or module name taking precedence in sys.path;
- missing module, class, function or variable;

Debugged import:

- 'config' not found.

Original exception:

ImportError: No module named 'config'

What I want:

From flask website.
>>>from yourapplication import User
>>> admin = User('admin', '[email protected]')

In my case-
>>>from app import Entry
>>>entry = Entry("title", "this is a post.")
4
  • is folder in your site-packages? have you ever run a setup.py for folder. also try before importing try adding import sys sys.path.insert(0, '/path/to/the/application') . Im thinking it should be more from folder.app.models import Entry Commented Jan 15, 2016 at 19:22
  • Why not just use from app.models import Entry? Commented Jan 15, 2016 at 19:30
  • I tried your suggestion (both) but no go. I now realize though that I was activated into another venv and playing around with the files you see before I realized it.. I'm going to try recreating the folders/etc and copying/pasting the code, though I don't see why they would have messed things up here. Commented Jan 15, 2016 at 19:33
  • possible that modules were loaded in one environment and not the other? For me I install a top level setup.py that installs all dependencies Commented Jan 15, 2016 at 19:36

1 Answer 1

1

from app.models import Entry

You must import Entry from the models module of the app.

You may also need to add relative imports in your __init__ for any custom modules.

add to your __init__

from .models import *

edit: Another useful debugging tip - is to use the dir() function to see what is included in a package. In your case, the imports are working so you can run something like

dir(app)

With an argument, attempt to return a list of valid attributes for that object.

for instance here is a returned list for a flask app I wrote.if You cant see your moudle here, add the import.

enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

This might help you with proper configuration from classes. Your config.py doesnt have any objects! damyanon.net/flask-series-configuration
I had replied too soon and deleted my posts hoping no one saw. I'm testing everything out now, but at the moment, 'from app.models import Entry' does work in the console. I'm seeing if that's the end of my problems or not.
Everything in the console is working! I can now use my model class. I never really thought about config files before, but I will give that link a good read. Changing the config to pyfile, from object, and moving the config.py to the app folder, fixed the error. One last question. When starting 'python3 models.py', I get an importerror as in the original post. When I change the statement to 'from . import db', the error becomes 'SystemError Parent module '' not loaded, cannot perform relative import'. Is this because I'm trying to run it directly or another problem?
only use relative imports in __init__. dont use from . in application code
PS. If anyone was wondering what that question was about (I asked it badly), basically I was trying to run the scripts directly (basically to test for errors), but I now realize that you're not supposed to do it like that. 'python3 -m models.py' ran from the top directory (in my case, folder), returns no errors and seems to be the proper way. Check the answer by JF Sabastian. stackoverflow.com/questions/27828319/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.