Search code examples
pythonflaskpycharmtraceback

Python tracebacks not visible for error leading to 500 Server Error


I normally can see Python tracebacks in the Pycharm "run" window. However, I have a project where I do not get any Python tracebacks showing in the "run" window for the Internal Server error (see below):

> /Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
> /Users/noname/PycharmProjects/usc/main.py  * Running on
> http://127.0.0.1:5000/ (Press CTRL+C to quit)
> 127.0.0.1 - - [06/Jul/2015 15:23:42] "GET / HTTP/1.1" 200 -
> 127.0.0.1 - - [06/Jul/2015 15:23:43] "GET /estimator HTTP/1.1" 200 -
> 127.0.0.1 - - [06/Jul/2015 15:23:44] "GET /estimator/errorform HTTP/1.1" 500 -

Initially I thought I must have accidentally changed some default settings and lost tracebacks completely. However I later realised that I do still get tracebacks showing in the "Run" window if I inject some arbitrary error in the code.

The problem is that I know that the 500 Server Error is caused by one of my Python instructions as I can get the error to disappear if I modify the instruction as commented below. I'm confused as to why I get the 500 Server Error, but not any Python tracebacks, though.

Relevant code snippet below:

@app.route('/estimator/errorform', methods=['GET', 'POST'])
def errorform():
    form = ErrorForm()
    line = str(open("logs.txt", "r").readlines()[int(file_len("logs.txt"))]).rstrip()  
    #Server Error disappears if I replace [int(file_len("logs.txt"))] with 1, above
    if form.validate_on_submit():
        webbrowser.open('mailto:[email protected]?subject=Feedback&body=
        <insert your message here>. \n\n Logs: %s' % (line))
    return render_template('main.html', form=form, show_results=0,page='errorform')

Solution

  • Try to set debug = True to your Flask app:

    app = Flask(__name__)
    app.debug = True
    

    See Flask documentation.