Search code examples
pythonflask-sqlalchemydaemonsflask-script

ValueError: Attempted relative import in non-package for running standalone scripts In Flask Web App


I have flask web app and its structure is as follows:

/app  
    /__init__.py  
    /wsgi.py
    /app  
        /__init__.py
        /views.py  
        /models.py 
        /method.py
        /common.py
        /db_client.py
        /amqp_client.py
        /cron
            /__init.py__
            /daemon1.py
            /daemon2.py
        /static/  
            /main.css
        /templates/  
            /base.html
    /scripts  
    /nginx
    /supervisor 
    /Dockerfile 
    /docker-compose.yml

In app/app/cron i have written standalone daemons which i want to call outside the docker. e.g. python daemon1.py

daemon1.py code

from ..common import stats

from ..method import msapi, dataformater

from ..db_client import db_connection

def run_daemon():

......

......

......

if name =="main":

run_daemon()

So when i am trying to run this daemon1.py its throwing ValueError: Attempted relative import in non-package

Please suggest the right approach for import as well as to structure these daemons.

Thanks in advance.


Solution

  • @greenbergé Thank you for your solution. i tried but didn't worked for me.

    So to make things work I have changed my code little bit. Apart from calling run_daemon() in main of daemon1.py, i have called function run_daemon() directly.

    python -m 'from app.cron.daemon1 import run_daemon(); run_daemon()'

    As it is not exact solution of the problem but things worked for me.