TAGS :Viewed: 5 - Published at: a few seconds ago

[ Reuse app object in Flask ]

I am trying to reuse the app object in flask within another module.

Currently my directory structure is:

/xampp/code/MenuMaster
    init.py
    menumaster
        init.py
        menumaster_app.py
        sqltables.py

<p><br/>
My first <code>__init__.py</code> file contains:</p>

app = Flask(name) app.config['SECRET_KEY'] = 'some key here'

<p>The second <code>__init__.py</code> file is empty.</p>

<p><code>menumaster_app.py</code> uses this app object. <code>sqltables.py</code> also needs to use this app object. <br/><br/></p>

<p>This is my <code>.wsgi</code> file:</p>

import sys sys.path.insert(0, 'C:/xampp/code/MenuMaster') from menumaster_app import app as application

<p>I am currently receiving the error in <code>.wsgi</code>:</p>

File "C:/xampp/htdocs/flaskapp/flask.wsgi", line 5, in <module> from menumasterapp import app as application ImportError: No module named menumasterapp

<p><br/></p>

<p>If I change my <code>flask.wsgi</code> file to:</p>

sys.path.insert(0, 'C:/xampp/code/Menumaster/menumaster')

<p>I get the error:</p>

File "C:/xampp/htdocs/flaskapp/flask.wsgi", line 5, in <module> from menumasterapp import app as application File "C:/xampp/code/Menumaster/menumaster\menumasterapp.py", line 44, in <module> @app.route('/restaurants', methods = ['GET']) NameError: name 'app' is not defined

<p>If I am completely going about this the wrong way, I would love to hear the proper method to accomplish this.</p>

<p>Any help on this matter would be appreciated.</p>

<p><strong>UPDATE</strong></p>

<p>I have changed my file structure to:</p>

menumasterproject menumaster init.py menumasterapp.py sqltables.py

<p><code>flask.wsgi</code></p>

import sys sys.path.insert(0, 'C:/xampp/code/menumaster_project') from menumaster import app as application

<p><code>__init.py__</code></p>

from flask import Flask app = Flask(name) app.config['SECRET_KEY'] = 'some key here'

```

However, I am currently getting a 404 error.

Answer 1


Your application is structured oddly. A more common layout would be:

menumaster_project/
├─── menumaster/
│   ├── __init__.py
│   ├── sqltables.py
│   └── my_subpackage/
│      ├── __init__.py
│      └── my_submodule.py
└── other_project_file.txt

Where menumaster_project would be on the Python path. The app would be defined in menumaster_project/menumaster/__init__.py. Importing app would be:

from menumaster import app as application