【问题标题】:auto importing python classes in flask shell not working as expected在烧瓶外壳中自动导入 python 类没有按预期工作
【发布时间】:2013-08-07 08:56:25
【问题描述】:

所以我有一个以这种方式构建的烧瓶应用程序

calvin % tree -L 3 .
.
├── README
├── alembic
│   ├── README
│   ├── env.py
│   ├── env.pyc
│   ├── script.py.mako
│   └── versions
├── alembic.ini
├── api.sublime-project
├── app
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── admin
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── agencies
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── models.py
│   │   └── models.pyc
│   ├── agents
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── models.py
│   │   ├── models.pyc
│   │   ├── views.py
│   │   └── views.pyc
│   ├── api.py
│   ├── api.pyc
│   ├── auth
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── constants.py
│   │   ├── constants.pyc
│   │   ├── decorators.py
│   │   ├── decorators.pyc
│   │   ├── models.py
│   │   ├── models.pyc
│   │   ├── views.py
│   │   └── views.pyc
│   ├── districts
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── models.py
│   │   ├── models.pyc
│   │   ├── views.py
│   │   └── views.pyc
│   ├── helpers.py
│   ├── helpers.pyc
│   ├── middleware.py
│   ├── middleware.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── properties
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── models.py
│   │   └── models.pyc
│   ├── templates
│   │   └── index.html
│   ├── users
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── models.py
│   │   ├── models.pyc
│   │   ├── views.py
│   │   └── views.pyc
│   └── viewings
│       ├── __init__.py
│       ├── __init__.pyc
│       ├── models.py
│       ├── models.pyc
│       ├── views.py
│       └── views.pyc
├── config.py
├── config.pyc
├── manage.py
├── requirements.txt
├── run.py
└── shell.py

我正在设置我的 shell,以便在我执行 ./manage.py shell 时自动导入位于 models.py 文件中的所有类

这是 manage.py 中的脚本,旨在实现这一目标(参考 flask-script 文档)

def _make_context():
    from app import models
    return dict(app=app, db=db, models=models)  # TODO: this is not working appropriately
manager.add_command("shell", Shell(make_context=_make_context))

在我的 app/models.py 中,我有来自每个模块、“代理”、“身份验证”等的导入语句。

但是,当我进入我的 shell 环境时,我必须以models.Users 而不是直接Users 访问我的类,这不是我所期望的。如何自动导入所有内容以便我可以直接访问这些类?

【问题讨论】:

    标签: flask flask-extensions


    【解决方案1】:

    下面是一个简化的解决方案,假设您使用的是 SQLAlchemy (db.Model)。如果没有,您应该只需要更改 issubclass if 语句以匹配您对要导入的内容的适当检查。

    from app import db, create_app
    import app.models as models
    from flask.ext.script import Manager, Shell 
    app = create_app()
    manager = Manager(app)
    
    def make_shell_context():
        return_dict = {}
        # grab everything we can possibly import from the models module
        module_importables = dir(models)
        for importable in module_importables:
            # if it isn't a class, it'll throw a TypeError exception that importable is not a class
            try:
                # we only want our SQLAlchemy models
                if issubclass(getattr(models,importable),db.Model):
                    return_dict[importable] = getattr(models,importable)
            except TypeError as inst:
                pass
        return_dict['app'] = app
        return_dict['db'] = db
        return return_dict
    
    manager.add_command("shell", Shell(make_context=make_shell_context))
    
    if __name__ == '__main__':
        manager.run()
    

    【讨论】:

      【解决方案2】:

      代替

      from app import models
      

      你可以这样做:

      from app.models import *
      

      这将从模型中导入所有类和变量。注意:通常不建议导入 * 但在这种情况下,它可能对您有用。理想情况下,您应该执行以下操作:

      from app.models import Users, ...and so on
      

      【讨论】:

      • 不,这不起作用,因为我必须将包含我想要的类的导入模块传递给 Shell 类的模块参数。
      • 传入模型参数,而不是模块。
      猜你喜欢
      • 2014-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-24
      • 1970-01-01
      • 2021-12-26
      • 2012-05-09
      相关资源
      最近更新 更多