Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import python class into another folder in a package

I'm trying to import a python class from another folder of my python package but I this error "No module named models".

It's 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've a class named: application

And in connection.py I want import my class "application". I've try with "from models import application" but I've this error "no modules named models".

I think it's possible to reach this goal with the modification of the init.py in the "updateto" folder but I'm no sure because I don't clearly understand the functioning of the init.py file...

Thanks

EDIT:

Which is weird, it's if under connection.py I add "import myapp" and "print(help(myapp))", I've 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've this error "ImportError: cannot import name models"

EDIT2:

If I try to add in connection.py "from myapp import app", it works but not if I try to import models... I'm very surprised. I've try to add in myapp/init.py:

__all__ = ["models", "routes]

But same error...

EDIT3:

hummmm I've move all my modules under the libs folder into myapp and when I try to put in connection.py "from model import application" I've always the same error "ImportError: cannot import name application"

Other test in connection.py:

import models
print(models)

Output:
<module 'myapp.models' from 'C:\app\myapp\models.pyc'>

I can import my module named models but I can't import the class under models.py

EDIT4:

Ok, I think I found the problem. It's my ini.py in myapp, because if I comment this file content, the connection.py can now import my class application in models. I run the file run.py which uses routes.py and it's him that calls operation.py

With some tests, I've find the lines who create my error:

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

But why????

like image 243
Matt Avatar asked Nov 22 '13 22:11

Matt


1 Answers

I don't know about Flask specifically, but in Python you should generally include a __init__.py in every folder and subfolder for them to be recognised as modules and/or packages.

In the diagram you provide, there is no __init__.py in the app folder. Try putting one and re-post if that resolves your problem.

If it doesn't work, try:

import os
print(os.getcwd())

If the current working directory isn't the app directory, you'll have to work around this. One way would be:

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

import myapp.models

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

Update: Sorry I just realized that maybe you' re trying to run connections.py as a script. That would change you working directory to that of connections.py. Try importing connections.py from your main script with import models inside connections.py and see if you get an error.


Update (in reply to Edit 4): (I don't have enough reputation points to comment yet, so I reply here)

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

The problem in this piece of code is the import myapp.routes. Change that to import routes and it should work. As a rule of thumb, your main script's directory is your root directory. You can import any module located in your root directory from inside any other module (even modules inside a sub-folder!) with a simple import module.

like image 79
Asotos Avatar answered Oct 30 '22 00:10

Asotos