ImportError: No module named application

This question already has an answer here:

  • What is __init__.py for? 11 answers

  • I think that with Python 2, you will have to put an empty

    __init__.py 
    

    file in your database directory


    First your database folder isn't a proper python package.

    Each package in Python is a directory which MUST contain a special file called __init__.py. This file can be empty, and it indicates that the directory it contains is a Python package, so it can be imported the same way a module can be imported. From learn python website

    Second (and this is just a suggestion), to enforce a proper namespace you should create a package named after your app inside of your project directory and on top of every other packages:

    myproject/
      requirements.txt
      setup.cfg
      ...
      myapp/
         __init__.py
         app2.py
         database/
             __init__.py
             dbconfig.py
    

    Note that package name double always be lowercase and if needed use underscores as separators instead of a dash.

    The namespace will be:

    myapp.database.dbconfig.db
    

    So from app2.py you can do for example:

    from .database.dbconfig import db
    

    Also your code declare the flask object both in app2.py and dbconfig.py. Therefore the application object in the app2.py module isn't bound to the Sqlalchemy object. You should declare your db in dbconfig then import it in app2.py and use the init_app method.

    Finally

    flat is better than nested

    Zen of python

    So this tree can be reduce to just a database.py containing the models and db object and the flask app object creation inside the myapp/__init__.py

    链接地址: http://www.djcxy.com/p/54814.html

    上一篇: 为什么eclipse为Python创建两个类文件?

    下一篇: ImportError:没有名为application的模块