0

I have changed my application running with flask and python2.7 from a standalone solution to flask with apache and mod_wsgi.

My Flask app (app.py) includes some classes which are in the directory below my app dir (../).

Here is my app.wsgi:


    #!/usr/bin/python
    import sys
    import logging
    logging.basicConfig(stream=sys.stderr)
    sys.stdout = sys.stderr
    
    
    project_home = '/opt/appdir/Application/myapp'
    project_web  = '/opt/appdir/Application/myapp/web'
    
    
    if project_home not in sys.path:
        sys.path = [project_home] + sys.path
    
    if project_web not in sys.path:
        sys.path = [project_web] + sys.path
    
    from app import app
    application = app
    

Before my configuration to mod_wsgi my main call in the app.py looks like that:


    # Main
    if __name__ == '__main__' :
        from os import sys, path
        sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
        from logger import Logger
        from main import Main
        from configReader import ConfigReader
        print "Calling flask"
        from threadhandler import ThreadHandler
        ca = ConfigReader()
        app.run(host="0.0.0.0", threaded=True)

I was perfectly able to load my classes in the directory below. After running the app with mod_wsgi I get the following error: global name \'Main\' is not defined

So how do I have to change my app that this here would work:


    @app.route("/")
    def test():
       main = Main("test")
       return main.responseMessage()
 
2
  • Have you tried to import Mainin your app wsgi?
    – sunwarr10r
    Dec 29, 2016 at 14:41
  • Nothing under that __main__ check will be executed under mod_wsgi as your script isn't executed as a main script. Anything in that which sets up sys.path must be done in app.wsgi or better still in the Apache configuration for mod_wsgi. Anything else such as importing modules needs to always be done at the top of any code file needing them. Dec 29, 2016 at 19:57

1 Answer 1

0

No main function call with mod_wsgi was the right answer. I do not implemented my required modules in the wsgi file, but on top of the flask app.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.