Search code examples
pythonpython-3.xflaskdeploymentpythonanywhere

How to deploy a Flask application on pythonanywhere with a folder as a module


Currently I am trying to a deploy my first FLASK application on PythonAnywhere.

Im not sure if this is the correct terminology but I have a folder as a module and there for I can't seem to find the correct way to deploy my application. I am not even sure where to start in resolving this issue. Any advice?

File and Folder Layout Snipped

my init.py code is:

import os

from flask import Flask


def create_app(test_config=None):
    # create and configure the app
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_mapping(
        SECRET_KEY='secret',
        DATABASE=os.path.join(app.instance_path, 'LAMA.sqlite'),
    )

    if test_config is None:
        # load the instance config, if it exists, when not testing
        app.config.from_pyfile('config.py', silent=True)
    else:
        # load the test config if passed in
        app.config.from_mapping(test_config)

    # ensure the instance folder exists
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass

    # database
    from . import db
    db.init_app(app)

    # authentication blueprint
    from . import auth
    app.register_blueprint(auth.bp)


    # blog blueprint - the main index
    # from . import blog
    # app.register_blueprint(blog.bp)
    # app.add_url_rule('/', endpoint='index')

    # book blueprint 
    from . import book
    app.register_blueprint(book.bp)
    app.add_url_rule('/', endpoint='index')

    return app

I have also followed the python debugging page where I have done the following:

>>> import LAMA
>>> print(LAMA)
<module 'LAMA' from '/home/ivanv257/LAMA_MAIN/LAMA/__init__.py'>

So at this stage in my WSGI configuration file I have:

import sys

path = '/home/ivanv257/LAMA_MAIN/LAMA/__init__.py'
if path not in sys.path:
    sys.path.append(path)

from LAMA import app as application

I have also tried many other combinations such as

path = '/home/ivanv257/LAMA_MAIN/LAMA/'
from init import app as application

path = '/home/ivanv257/LAMA_MAIN/'
from init import app as application

path = '/home/ivanv257/LAMA_MAIN/'
from LAMA import app as application

my source code path is : /home/ivanv257/LAMA_MAIN/LAMA , although I have also tried different combinations such as /home/ivanv257/LAMA_MAIN/

ERROR DETAIL:

2018-12-08 10:05:32,028: Error running WSGI application
2018-12-08 10:05:32,030: ModuleNotFoundError: No module named 'LAMA'
2018-12-08 10:05:32,030:   File "/var/www/ivanv257_pythonanywhere_com_wsgi.py", line 83, in <module>
2018-12-08 10:05:32,030:     from LAMA import app as application  # noqa

Solution

  • To solve my problem I changed the following (with some assistance) from lama import create_app:

    import sys
    
    path = '/home/ivanv257/LAMA_MAIN/LAMA'
    if path not in sys.path:
        sys.path.append(path)
    
    from lama import create_app
    
    application = create_app()
    

    I also had to remove the from . to just imports

        import db
        db.init_app(app)
    
        # authentication blueprint
        import auth
        app.register_blueprint(auth.bp)