ImportError:没有名为app的模块

时间:2015-02-19 20:20:31

标签: python flask python-import flask-testing

我使用Flask-Testing并使文件test_app.py进行测试但是我从app import create_app获得了此错误文件“test_app.py”,第4行,db ImportError:没有名为app的模块。 所以请帮助我如何解决它以及问题Thanx :)

这是我的结构:

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

1 个答案:

答案 0 :(得分:8)

来自评论:

可能存在两个问题:

  1. myapplication/的路径未添加到$PYTHONPATH环境变量中(更多信息herehere) 我们假设代码位于/home/peg/myapplication/之下。您需要输入终端 export PYTHONPATH=${PYTHONPATH}:/home/peg/myapplication/

  2. __init__.py可能有拼写错误。下划线__和 init.py 块之间不应该有空格(__init__.py是好的,__ init __.py不是)