Import python class to another folder in package

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????

+6
source share
1 answer

I don’t know about Flask specifically, but in Python you should usually include __init__.py in each folder and subfolder to recognize them as modules and / or packages.

In the above diagram, there is no __init__.py in the app folder. Try to place one and reposition if this solves your problem.

If this does not work, try:

 import os print(os.getcwd()) 

If the current working directory is not an app directory, you will have to get around this. One of the methods:

 import sys sys.path.append('/path/to/app') import myapp.models 

You can always see which directories are in your search path for modules by checking sys.path .

Update: Sorry, I just realized that maybe you are trying to run connections.py as a script. This will change your working directory to the connections.py directory. Try importing connections.py from the main script using import models inside connections.py and see if you have an error.


Update (in response to "Edit 4"): (I have no comments for comments yet, so I reply here)

 from models import db db.init_app(app) import myapp.routes 

The problem with this piece of code is import myapp.routes . Change this to import routes and it should work. Typically, your main script directory is your root directory. You can import any module located in the root directory from any other module (even modules inside a subfolder!) With a simple import module .

+2
source

Source: https://habr.com/ru/post/958678/


All Articles