简体   繁体   中英

importing current_app from celery is gives ValueError: attempted relative import beyond top-level package

Yes I know there are lot's of similar questions on stack-overflow related to this value-error and I tried all the solutions from them but as I am new to Django and python I am not able to solve this issue.

I have one project called my_backend which have the following file structure.

main_project/
   cmb_backend/
        __init__.py
        celery.py
        urls.py
    second_app/
        __init__.py
        moduleZ.py
    my_env/
       bin/
       include/
       lib/
         python 3.7/
           site-packages/
             celery/
             django_celery_beat
               admin.py

I have used celery for the periodic task so I have added one celery.py file in my main application my_backend.

I have also installed django_celery_beat using pip and inside that, they have imported celery using below code.

# admin.py file in the django_celery_beat lib
from celery import current_app
from celery.utils import cached_property

so when I run this command

python3 my_backend/setup_database.py

it is giving me an error like

ImportError: cannot import name 'current_app' from 'celery' (/Users/pankaj/Desktop/Pankaj/MyJangoProjects/My_Project/my_backend/celery.py)

so from this error, I found that when I am running above command admin.py is importing current_app from celery but it is looking in the wrong file

so to solve this error I am using relative import and adding .. in front of import statement but still, it's not working

# admin.py file in the django_celery_beat lib
from ..celery import current_app
from ..celery.utils import cached_property

Now here I am getting ValueError: attempted relative import beyond top-level package

I have tried some sys.path hack but my bad, I am still stuck here.

I have also checked all these questions and tried solutions which are provided there.

beyond top level package error in relative import

Sibling package imports

Relative imports for the billionth time

How to do relative imports in Python?

Attempted relative imports beyond top-level package?

Your error is probably related to the celery.current_app is somewhat not included in your PYTHONPATH . Make sure these two items are satisfied:

  1. Make sure celery.current_app package is installed somewhere under your PYTHONPATH .
  2. If you are using virtual environment, make sure celery is installed and your virtual environment is activated.

I found the solution to this problem. As I mentioned the issue was the same name of the file in my main app, as the file name was celery.py when I run django_celery_beat it was looking for current_app inside my custom celery.py file so I have changed the file name to my_task.py as the @cagrias suggested.

I have appended app name in the below command and it's working now.

celery -A my_backend.task worker --loglevel=info

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM