Neo
Neo

Reputation: 47

Importing .py file is breaking flask app

I have a very simple flask app that imports a py file in a different folder and returns output. Here is my flash app code:

from flask import Flask, request
app = Flask(__name__)

@app.route('/login', methods=['POST'])
def flask_test():
    # Imports login.py from python/libs folder
    from python.libs import login
    # input coming from a .php file 
    host = request.form.get('Distro','')
    #simple py file to test login and return pass or fail
    result = login.telnet_host(host)
    return result

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

I know code and flask works because if I comment out import line, and add some test code I get a correct response. I also created a test.py file on same folder and used same import command and running "python test.py" works fine as well. However for some reason, import statement in flask.py file is not working.

if I do " python flask_home.py" I get no errors and see this:

* Running on http://127.0.0.1:5000/

but when I go to web and add the host name in host filed and hit submit it gives me 500 server error. Again, If I comment out import line and add test code it works. I even moved file in same folder and modified import line to "import login" but it still wouldn't work.

Does anybody knows why import is breaking it? My flask.py file is in /var/www and my login.py file is in /var/www/python/libs. I can not move both in same folder unfortunately. Please advice.

Thanks

Upvotes: 0

Views: 921

Answers (1)

Thiago F. Pappacena
Thiago F. Pappacena

Reputation: 326

In the same directory of flask_home.py, enter python shell and try to import from python.libs import login to see if you have syntax error in your login.py file.

Make sure you didn't forget to put __init__.py files in python/ and python/libs/ folders.

Upvotes: 1

Related Questions