7

I working with Flask-Testing and make file test_app.py to test But I got this error File "test_app.py", line 4, in from app import create_app, db ImportError: No module named app. so please help how can I fix it and what is the problem Thanx :)

here is my Structure:

myapplication
 app
    __ init __.py
    model.py
    form.py
    autho
    layout
    static
    templates
 migrations  
 test
   -test_app.py
 config.py
 manage.py

test_app.py

#!flask/bin/python
import unittest
from flask.ext.testing import TestCase
from app import create_app, db
from app.model import Users
from flask import request, url_for
import flask

class BaseTestCase(TestCase):

    def create_app(self):
        self.app = create_app('testing')
        return self.app

config.py

class TestingConfig(Config):
    TESTING = True
    SQLALCHEMY_DATABASE_URI = os.environ.get('TEST_DATABASE_URL') or \
        'sqlite:///' + os.path.join(basedir, 'mytest.sqlite')

__ init __.py

#!flask/bin/python
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.login import LoginManager
import psycopg2
from config import basedir
from config import config

db = SQLAlchemy()
lm = LoginManager()
lm.login_view = 'login'
login_manager = LoginManager()
login_manager.login_view = 'layout.login'

def create_app(config_name):
    app = Flask(__name__)
    app.config['DEBUG'] = True
    app.config.from_object(config[config_name])
    db.init_app(app)
    login_manager.init_app(app)
    # login_manager.user_loader(load_user)
    from .layout import layout as appr_blueprint
    # register our blueprints
    app.register_blueprint(appr_blueprint)

    from .auth import auth as auth_blueprint
    app.register_blueprint(auth_blueprint)
    return app
LiLi
  • 301
  • 2
  • 7
  • 21
  • You might need to add your `myapplication/` directory to the `$PYTHONPATH` (see http://users-cs.au.dk/chili/PBI/pythonpath.html ) – Savir Feb 19 '15 at 21:01
  • thnx for ur comment but im not expert in python so should I command (venv)peg@peg:~/myapplication/test$ export PYTHONPATH=${PYTHONPATH}:/users/[your username]/myapplication/test/ ??? @BorrajaX – LiLi Feb 19 '15 at 21:21
  • Almost. Try: `export PYTHONPATH=${PYTHONPATH}:/users/[your username]/myapplication/` Your `app` module (or directory) is under `/myapplication/`. Let us know what happens – Savir Feb 19 '15 at 21:28
  • I mean... Just to be clear... The path to add to the `$PYTHONPATH` is the full path to the `myapplication` directory. Let's say it's under `/home/borrajax/Documents/Python/myapplication/`, then you'd do `export PYTHONPATH=${PYTHONPATH}:/home/borrajax/Documents/Python/myapplication/` (this probably varies for machines with Windows, though) – Savir Feb 19 '15 at 21:32
  • Wait... There's a whitespace between the two underscores and `init` in `__ init.py __' and another one between `init.py` and the two final underscores. Is that a type or is it that like that in your computer? 'Cuz it shouldn't have whitespaces `__ init.py __` is not correct. `__init.py__` is. – Savir Feb 19 '15 at 21:41
  • it works @BorrajaX :)))) make it as an answer and Thanks – LiLi Feb 19 '15 at 21:45
  • export PYTHONPATH=${PYTHONPATH}:/home/peg/myapplication/ – LiLi Feb 19 '15 at 21:47

1 Answers1

11

From the comments:

There could have been two issues:

  1. The path to myapplication/ hadn't been added to the $PYTHONPATH environment variable (more info here and here) Let's say the code lives under /home/peg/myapplication/. You need to type in your terminal export PYTHONPATH=${PYTHONPATH}:/home/peg/myapplication/

  2. __init__.py could have had a typo. There shouldn't be whitespaces between the underscores __ and the init.py chunk (__init__.py is good, __ init __.py is not)

Community
  • 1
  • 1
Savir
  • 17,568
  • 15
  • 82
  • 136