Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Loading commands from modules: '"There is no app here. This is unlikely to work."' #145

Open
@nfarrar

Description

@nfarrar

I attempted to refactor my application so that commands are stored in individual modules and loaded by a function. I thought this should work, but the following exceptions are being raised - and I'm not quite sure why:

Traceback (most recent call last):
  File "./manage.py", line 14, in <module>
    register_commands(app, manager)
  File "~/Documents/Projects/flaskproject/cli.py", line 51, in register_commands
    cmd_mod = importlib.import_module(cmd_mod_str)
  File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "~/Documents/Projects/flaskproject/commands/debug.py", line 14, in <module>
    'debug': DebugCommand()
  File "~/.cache/virtualenvs/project/lib/python2.7/site-packages/flask_script/__init__.py", line 148, in __call__
    raise Exception("There is no app here. This is unlikely to work.")
Exception: There is no app here. This is unlikely to work.

Here's the basic structure/pattern I'm attempting to use (irrelevant bits removed):

manage.py:

from flaskapp.app import create_app
from flaskapp.cli import create_manager

app = create_app()
manager = create_manager(app)

if __name__ == '__main__':
    manager.run()

flaskapp/app.py:

from flask import Flask
from .config import configure_app
from .extensions import configure_extensions

def create_app(cfg=None):
    app = Flask(__name__)
    configure_app(app)
    configure_extensions(app)
    return app

flaskapp/cli.py:

def create_manager(app):
    manager = Manager(app, with_default_commands=False)

    env = app.config['ENV']
    root_pkg = os.path.basename(app.config['APP_ROOT'])
    pkg_path = os.path.join(root_pkg, cmd_dir)
    pkg_name = root_pkg + '.' + cmd_dir

    for _, mod_name, _ in pkgutil.iter_modules([pkg_path]):
        mod_name =  pkg_name + '.' + mod_name
        mod = importlib.import_module(mod_name)
        if hasattr(mod, 'commands'):
            for name, cmd in mod.commands.items():
                manager.add_command(name, cmd)

    return manager

flaskapp/commands/clean.py:

from flask_script.commands import Clean

commands = {
    'clean': Clean()
}

The purpose is so that I can just drop a new module containing a new command to add to the application in the commands dir - and it's completely self-contained - it'll be loaded without me having to jump around and modify several other files.

Any suggestions?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions