2

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?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Pankaj K.
  • 535
  • 8
  • 18

2 Answers2

1

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.
cagrias
  • 1,847
  • 3
  • 13
  • 24
  • thanks for the reply, let me try your solution and will get back to you – Pankaj K. Aug 19 '19 at 07:33
  • Nope, still same issue, everything is working fine if I just rename my custom celery.py file and execute the same command. the issue is when python tries to load module in admin.py file it is looking inside my custom celery.py file, not inside the env/lib/celery module. – Pankaj K. Aug 19 '19 at 08:22
  • Why do you chose naming your file as `celery.py`? This is confusing and not safe. – cagrias Aug 19 '19 at 08:25
  • yes I get to know this, I was following this site for the celery installation so I have used it. https://realpython.com/asynchronous-tasks-with-django-and-celery/ – Pankaj K. Aug 19 '19 at 08:33
  • common approach for adding celery tasks is naming `tasks.py`. See their official tutorial http://docs.celeryproject.org/en/latest/getting-started/first-steps-with-celery.html#application – cagrias Aug 19 '19 at 08:34
1

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
Pankaj K.
  • 535
  • 8
  • 18