Search code examples
pythonflask

Having Trouble Using Flask CLI, Module Not Found


Why can't Flask find the 'flask_app.config.DevelopmentConfig' in the app.config.from_object(environment_import_path) line (in the application factory below)?

In the application factory (see below), I have set instance_relative_config to True and the instance_path to the be the src package/directory. I've also tried the basic app = Flask(__name__), but I get the same error.

I'm running Python 3.12 and Flask 3.0.3. I'm stuck. When I run the app with run_flask_app.py it works fine. What am I missing?

While in the src directory, I run

flask --app 'flask_app.app:create_app("flask_app.config.DevelopmentConfig")' run

and get an error:

environment_import_path flask_app.config.DevelopmentConfig
basedir /Users/victorreichert/Documents/Python/CFNFT_PROJ/src/flask_app
basedir2 /Users/victorreichert/Documents/Python/CFNFT_PROJ/src
app.instance_path /Users/victorreichert/Documents/Python/CFNFT_PROJ/src
Traceback (most recent call last):
  File "/Users/victorreichert/Documents/Python/CFNFT_PROJ/src/venv/bin/flask", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/Users/victorreichert/Documents/Python/CFNFT_PROJ/src/venv/lib/python3.12/site-packages/flask/cli.py", line 1105, in main
    cli.main()
  File "/Users/victorreichert/Documents/Python/CFNFT_PROJ/src/venv/lib/python3.12/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/Users/victorreichert/Documents/Python/CFNFT_PROJ/src/venv/lib/python3.12/site-packages/click/core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/victorreichert/Documents/Python/CFNFT_PROJ/src/venv/lib/python3.12/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/victorreichert/Documents/Python/CFNFT_PROJ/src/venv/lib/python3.12/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/victorreichert/Documents/Python/CFNFT_PROJ/src/venv/lib/python3.12/site-packages/click/decorators.py", line 92, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/victorreichert/Documents/Python/CFNFT_PROJ/src/venv/lib/python3.12/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/victorreichert/Documents/Python/CFNFT_PROJ/src/venv/lib/python3.12/site-packages/flask/cli.py", line 953, in run_command
    raise e from None
  File "/Users/victorreichert/Documents/Python/CFNFT_PROJ/src/venv/lib/python3.12/site-packages/flask/cli.py", line 937, in run_command
    app: WSGIApplication = info.load_app()
                           ^^^^^^^^^^^^^^^
  File "/Users/victorreichert/Documents/Python/CFNFT_PROJ/src/venv/lib/python3.12/site-packages/flask/cli.py", line 335, in load_app
    app = locate_app(import_name, name)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/victorreichert/Documents/Python/CFNFT_PROJ/src/venv/lib/python3.12/site-packages/flask/cli.py", line 264, in locate_app
    return find_app_by_string(module, app_name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/victorreichert/Documents/Python/CFNFT_PROJ/src/venv/lib/python3.12/site-packages/flask/cli.py", line 178, in find_app_by_string
    app = attr(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/victorreichert/Documents/Python/CFNFT_PROJ/src/flask_app/app.py", line 42, in create_app
    app.config.from_object(environment_import_path)
  File "/Users/victorreichert/Documents/Python/CFNFT_PROJ/src/venv/lib/python3.12/site-packages/flask/config.py", line 254, in from_object
    obj = import_string(obj)
          ^^^^^^^^^^^^^^^^^^
  File "/Users/victorreichert/Documents/Python/CFNFT_PROJ/src/venv/lib/python3.12/site-packages/werkzeug/utils.py", line 612, in import_string
    raise ImportStringError(import_name, e).with_traceback(
  File "/Users/victorreichert/Documents/Python/CFNFT_PROJ/src/venv/lib/python3.12/site-packages/werkzeug/utils.py", line 604, in import_string
    module = __import__(module_name, globals(), locals(), [obj_name])
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
werkzeug.utils.ImportStringError: import_string() failed for 'flask_app.config.DevelopmentConfig'. Possible reasons are:

- missing __init__.py in a package;
- package or module path not included in sys.path;
- duplicated package or module name taking precedence in sys.path;
- missing module, class, function or variable;

Debugged import:

- 'flask_app' not found.

Original Exception:

ModuleNotFoundError: No module named 'flask_app'

My application has the following structure:

src
|-__init__.py
|-run_flask_app.py
|-flask_app
  |-__init__.py
  |-app.py (file name is app.py in the flask_app package, just to be clear)
  |-config.py

In the app.py I have an application factory:


def create_app(environment_import_path):
    print('environment_import_path', environment_import_path)
    print('basedir',  os.path.abspath(os.path.join('../', os.path.dirname(__file__))))
    print('basedir2',  os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

    app = Flask(__name__, instance_relative_config = True,
                instance_path=os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

    print('app.instance_path', app.instance_path)

    app.config.from_object(environment_import_path)

    db.init_app(app)
    migrate.init_app(app, db)

    register_blueprints(app)

    return app

run_flask_app.py

from flask_app.app import create_app

environment_import_path = 'flask_app.config.DevelopmentConfig'

app = create_app(environment_import_path)

if __name__ == '__main__':

    app.run(host="localhost", port=8000)

Solution

  • Per Miguel's comment above, I needed to add the src directory to the PYTHONPATH.