I am trying to import a python class from another folder of my python package, but I have this error "No module named models".
This is a Flask project.
-app |-run.py |-myapp |- libs |- updateto |- __init__.py (empty) |- connection.py |- removeto |- __init__.py (empty) |- static |- templates |- __init__.py (NOT empty) |- routes.py |- models.py
My init .py:
from flask import Flask app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///maindb.db' from models import db db.init_app(app) import myapp.routes
In my models.py, I have a class called: application
And in connection.py I want to import my class application. I am trying to use "from a model import application", but I have this error "without modules with model names".
I think it is possible to achieve this goal by changing init.py in the "updateto" folder, but I'm not sure because I don’t understand how the init .py file works ...
thanks
EDIT:
Which is strange, if I add "import myapp" and "print (help (myapp))" in connection.py, I have this output:
Help on package myapp: NAME myapp FILE c:\app\myapp\__init__.py PACKAGE CONTENTS libs (package) models routes DATA app = <Flask 'myapp'>
But if I try “from myapp import models”, I have this error “ImportError: cannot import name models”
EDIT2:
If I try to add “myapp” from the import application to connection.py, it works, but not if I try to import models ... I am very surprised. I am trying to add to myapp / init .py:
__all__ = ["models", "routes]
But the same mistake ...
EDIT3:
hummmm I move all my modules in the libs folder in myapp, and when I try to put in connection.py “from the model import application”, I always get the same error “ImportError: cannot import name application”
Another test in connection.py:
import models print(models) Output: <module 'myapp.models' from 'C:\app\myapp\models.pyc'>
I can import my module with model names, but I cannot import a class under models.py
EDIT4:
Ok, I think I found the problem. This is my ini .py in myapp, because if I comment on the contents of this file, connection.py can now import my class application into the model. I run the run.py file that uses routes.py and it calls the operation.py call
With some tests, I found lines that create my error:
from models import db db.init_app(app) import myapp.routes
But why????