Error on Module Flask Login

1.2k Views Asked by At

I'm new on flask/python dev but I need to use Powerdns-admin for my public DNS. This work with flask and i need to use supervisor to start the web panel of powerdns-admin. I think I made few mistakes on directory or launching flask.

This is my /opt/powerdns-admin/run.py

#!/usr/bin/env python
from app import app
from config import PORT

try:
        from config import BIND_ADDRESS
except:
        BIND_ADDRESS = '127.0.0.1'

if __name__ == '__main__':
    app.run(debug = True, host=BIND_ADDRESS, port=PORT)

Here the 12 first lines of /opt/powerdns-admin/app/init.py

from werkzeug.contrib.fixers import ProxyFix
from flask import Flask, request, session, redirect, url_for
from flask_login import LoginManager
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config.from_object('config')
app.wsgi_app = ProxyFix(app.wsgi_app)

login_manager = LoginManager()
login_manager.init_app(app)
db = SQLAlchemy(app)

And this is my supervisor config

[program:powerdnsadmin]
command = python /opt/powerdns-admin/run.py
directory = /opt/powerdns-admin/app
autostart = true
autorestart = true
stdout_logfile=/var/log/supervisor/program_powerdnsadmin.log
stderr_logfile=/var/log/supervisor/program_powerdnsadmin.error

But when I do a supervisorctl update i got this on the program_powerdnsadmin.error

Traceback (most recent call last):
  File "/opt/powerdns-admin/run.py", line 2, in <module>
    from app import app
  File "/opt/powerdns-admin/app/__init__.py", line 3, in <module>
    from flask_login import LoginManager
ImportError: No module named flask_login

However, when I launch the run.py manually it's OK

infra@nameserver:~$ source ./flask/bin/activate
(flask) infra@nameserver:~$ python powerdns-admin/run.py
[INFO]  * Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)

This is few versions :

 pip show python
Name: Python
Version: 2.7.12
 pip show flask
Name: Flask
Version: 0.11.1
 pip show flask_login
Name: Flask-Login
Version: 0.3.2

I hope the solution is a dumb thing, I'm blocked since 4 days on that :(

Thanks

2

There are 2 best solutions below

1
On BEST ANSWER

Your supervisor configuration is launching your system Python instead of your flask virtualenv Python.

The supervisor command must be:

command = ~/flask/bin/python /opt/powerdns-admin/run.py

I'm not sure if the ~ works, maybe you have to expand it.

0
On

You seem to use a virtual environment for your flask application. However in your supervisor config you start the app with your global python installation.

A solution is to either install all necessary packages to your global Python version or you start your virtual environment from supervisor and run your application there.

I prefer the latter as it is clearer if you have multiple Flask apps. An easy way is to create a run.sh within the direactory of your Flask application with the following content:

#!/bin/bash
source ./flask/bin/activate
exec python powerdns-admin/run.py

Don't forget to make this script executable via $ chmod a+x run.sh and replace your command setting in the supervisor config file with the filename of this script. This way supervisor will startup your Flask Application in the virtual environment.