1

Well, I have a Flask, SQL-Alchemy project. So far, this is my structure:

app
  |__ module1
  |    |__ models.py
  |    |__ views.py
  |__ module2
  |    |__ models.py
  |    |__ views.py
  |__ app.py
  |__ config.py

On every models.py, I have this:

from app import db

#classes

On every views.py, I have this:

from module1.models import *
from module2.models import *
from app import app
from flask import session, request, render_template, url_for, redirect
import datetime

#views

This is my app.py file:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import os

app = Flask(__name__)
db = SQLAlchemy(app)

from module1.views import *
from module2.views import *

import config

def init():
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port, debug=True)

if __name__ == "__main__":
    init()

When I'm on module1.views, and call a model from module2.models, works perfect. But when calling a model from module1.models on module1.views, I get a NameError:

module1.models, module1.views --> Works
module1.models, module2.views --> Works
module2.models, module1.views --> Name Error
module2.models, module2.views --> Works

Also, the import goes well, no error on that. When I call the class, it fails. I think it's some problem with the imports statements, but I don't know how to fix it. Any ideas?

Thank you guys, in advance

Diana
  • 140
  • 1
  • 13
  • 1
    It looks like you might want to be using the [blueprint](http://flask.pocoo.org/docs/1.0/blueprints/) feature – roganjosh Oct 29 '18 at 18:28

1 Answers1

0

You might need an __init__.py file in each of the sub directories https://docs.python.org/3/tutorial/modules.html#packages.

There's more info in this answer as well that might be helpful What is __init__.py for?

Also based on the way you are sharing model functionality between views it would probably make more sense to use a directory structure like

.
├── models
│   ├── models1.py
│   └── models2.py
└── views
    ├── view1.py
    └── view2.py
  • I just tried this, the documentation says it should be done with an empty __init__.py file, but it keeps failing. Any idea why? – Diana Oct 29 '18 at 18:34
  • This is the error: `NameError: name 'Cita' is not defined`, where Cita is the class implemented on module1.models. I'm calling it from module2.views, like `Cita.query.all()`. Also, when I call it from module1.views, works perfectly fine – Diana Oct 29 '18 at 19:01
  • Also, I thought it was because of the names, after reading the `__init__.py ` documentation, so I renamed those files. Still the same error. – Diana Oct 29 '18 at 19:04
  • What happens if you try to import it explicitly in module2.views? `from module1.models import Cita` – Spencer Post Oct 29 '18 at 19:11
  • when I do that I get this `ImportError: cannot import name 'Cita' from 'module1.models' (/app/module1/models.py)` – Diana Oct 30 '18 at 19:38
  • This answer https://stackoverflow.com/questions/9252543/importerror-cannot-import-name-x might be helpful as well now that it's narrowed down to an ImportError – Spencer Post Oct 31 '18 at 20:05
  • But I need to import the whole model. I tried to fix it by moving some of the models to the other module, but when I call a foreign key, all I get is blank. any ideas? – Diana Nov 04 '18 at 03:32