Home > Net >  Internal server error in flask application when I try to import pandas
Internal server error in flask application when I try to import pandas

Time:01-02

I'm trying to import pandas in my flask application. The moment I do that, I get an internal server error. I'm running my application on a Digital Ocean droplet (Ubuntu 20.0.4).

Server: nginx

Everything runs as expected if I don't import pandas. I have installed pandas through pip install already.Request you to assist me with this :)

Here's the snippet:

from flask import Flask, render_template, url_for, request
app = Flask(__name__, template_folder="templates")
import pandas as pd


@app.route("/")
def home():
    try: 
        return render_template('home.html')
    except Exception as e:
        return str(e)


if __name__ == "__main__":
    app.run(host='0.0.0.0')

CodePudding user response:

As per our discussion, you have to import the packages inside the if block, like this.

...
if __name__ == "__main__":
    import pandas as pd
    ...

I may only guess with the limited information that this works because of how the default development server works in flask. Some of the time the routing of each request is handled by a new spawned/forked process, when the system is something that is resource constrained, importing heavy packages on all the processes may end up taking up the resource limit and hence killing the process or the applet rendering server error.

Glad this helped.

  • Related