Flask deployment issue with imports

January 16, 2017 876 views
Apache Ubuntu 16.04

I followed this guide.

Most of my files are almost identical to the guide (except FlaskApp is changed to the project name HopUpon). except for __init__.py which looks like this:

from flask import *
import json
#import airport_finder2
#import pandas as pd
app = Flask(__name__)

@app.route('/')
def index():
  return render_template('quicksearch.html')

if __name__ == '__main__':
  app.run(debug=True)

This file works but when I uncomment airport_finder2 or pandas as pd I am unable to reach the website. It endlessly searches.

some background on airportfinder2:
this is a python file which searches through two csv files and returns a result.
These files are located in the same folder as``` _
init__.py``` - /var/www/HopUpon/HopUpon
but I would like to move them to /var/www/HopUpon/HopUpon/static once I can get them working.

pandas:
I have pip installed pandas on to the virtual environment.

wsgi:
I have tried to create a log but I get an internal server error when I do.

import logging
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(levelname)-8s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S',
                    filename='/temp/myapp.log',
                    filemode='w')
logging.debug('A debug message')

So I have no error logs to show. Apologies. I know it would make it much easer.

My questions:
Why are these imports giving errors?
What errors are they giving?
How do I run my Flask app in debug mode on digitalocean?

Any help will be greatly appreciated.

2 Answers

hey I figured out my errors.

  1. Hanging request was due to the .conf file. add the code under WSGIScriptAlias

    WSGIApplicationGroup %{GLOBAL}
    
  2. My second error came from trouble my python file had finding a csv file from which it produced results.
    Interestingly accessing a csv file on a server with python requires you to find the temp location of that file. which is not the same as the static location.
    so i referred to my cvs inside __init__.py with:

    from pandas import pandas as pd
    import os
    

HERE = os.path.abspath(os.path.dirname(file))
file1 = os.path.join(HERE, 'file1.csv')
file2 = os.path.join(HERE, 'file2.csv')

results1 = pd.readcsv(file1)
results2 = pd.read
csv(file2)


I hope this helps others.

@dougcreighton

To run Flask in debug mode, from the CLI, run:

export FLASK_DEBUG=1

and then

flask run

Keep in mind, once you log out of the current session, you'll need to run the same export again as it's only valid for the current session (i.e. you'll need to run that each time you login).

As for the import, the only details I can find on airport_finder point to airport_finder, as in, without the 2, so have you tried to import using airport_finder instead of airport_finder2?

  • Thanks. this was helpful. The airport_finder error was solved as you said it would be.

    I created a virtual environment then ran the code you put above which gives me the following response:

    (venv) root@HopUpon:/var/www/HopUpon/HopUpon export FLASK_DEBUG=1
    (venv) root@HopUpon:/var/www/HopUpon/HopUpon# flask run
     * Forcing debug mode on
     * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
     * Restarting with stat
     * Debugger is active!
     * Debugger pin code: 272-220-000
    

    However I am still getting an error from the website.
    "Internal Server Error"

    I dont know if this will help but I will include it anyways.
    here is my apache error.log

    [Thu Jan 19 06:19:51.181940 2017] [wsgi:warn] [pid 2120:tid 140508087519104] mod_wsgi: Compiled for Python/2.7.11.
    [Thu Jan 19 06:19:51.182501 2017] [wsgi:warn] [pid 2120:tid 140508087519104] mod_wsgi: Runtime using Python/2.7.12.
    [Thu Jan 19 06:19:51.183160 2017] [mpm_event:notice] [pid 2120:tid 140508087519104] AH00489: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/2.7.12 configured -- resuming normal operations
    [Thu Jan 19 06:19:51.183212 2017] [core:notice] [pid 2120:tid 140508087519104] AH00094: Command line: '/usr/sbin/apache2'
    [Thu Jan 19 06:22:18.373353 2017] [mpm_event:notice] [pid 2120:tid 140508087519104] AH00491: caught SIGTERM, shutting down
    [Thu Jan 19 06:22:19.444503 2017] [wsgi:warn] [pid 2266:tid 140282900223872] mod_wsgi: Compiled for Python/2.7.11.
    [Thu Jan 19 06:22:19.444577 2017] [wsgi:warn] [pid 2266:tid 140282900223872] mod_wsgi: Runtime using Python/2.7.12.
    [Thu Jan 19 06:22:19.445429 2017] [mpm_event:notice] [pid 2266:tid 140282900223872] AH00489: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/2.7.12 configured -- resuming normal operations
    [Thu Jan 19 06:22:19.445465 2017] [core:notice] [pid 2266:tid 140282900223872] AH00094: Command line: '/usr/sbin/apache2'
    

    Is there anything I can do to provide you with more data?
    Thanks for your help in advance

Have another answer? Share your knowledge.