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.")
folder
in yoursite-packages
? have you ever run asetup.py
forfolder
. also try before importing try addingimport sys sys.path.insert(0, '/path/to/the/application')
. Im thinking it should be morefrom folder.app.models import Entry
– Busturdustfrom app.models import Entry
? – dirn