#πŸ”’ Added default import path to script

6 messages Β· Page 1 of 1 (latest)

wooden coyote
#

Hey! I am setting up some celery workers, and they need to access some logic from my main app

The structure looks like this

fastapi-server
            /app/...
            /celery_task
                        /tasks/...
                        /__init__.py

In the init file in the celery tasks folder i am trying to setup the PATH such that all the tasks in the tasks folder is able to use the app folder, this would make it so i wouldn't have to update all my tasks in the future

The following is the current init file setup

import sys
from pathlib import Path

# Ensure the project root (fastapi-server/) is on sys.path so that 'app' and
# all other local packages are importable inside Celery's ForkPoolWorker
# processes.  This runs once when the celery_task package is first imported.
_project_root = str(Path(__file__).resolve().parent.parent)
if _project_root not in sys.path:
    sys.path.insert(1, _project_root)

from celery import Celery
from celery.schedules import crontab
from core.config import config

celery_app = Celery(
    "worker",
    backend=config.CELERY_BACKEND_URL,
    broker=config.CELERY_BROKER_URL,
)

celery_app.conf.task_routes = {
    "celery_task.tasks.cleanup_old_errors.cleanup_old_errors": "maintenance-queue",
    "celery_task.tasks.recur_overdue_tasks.recur_overdue_tasks": "maintenance-queue",
}

celery_app.conf.update(task_track_started=True)

# Add beat schedule
celery_app.conf.beat_schedule = {
    'cleanup-old-errors-daily': {
        'task': 'celery_task.tasks.cleanup_old_errors.cleanup_old_errors',
        'schedule': crontab(hour="2", minute="0"),
    },
    'recur-overdue-tasks-hourly': {
        'task': 'celery_task.tasks.recur_overdue_tasks.recur_overdue_tasks',
        'schedule': crontab(minute="0"),  # top of every hour
    },
}

# Import tasks to register them
from celery_task.tasks import cleanup_old_errors, recur_overdue_tasks  # noqa: F401, E402

The current solution i came up with is in regards to

limpid pollenBOT
#

@wooden coyote

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

wooden coyote
#

This is the error returned from celery

[2026-02-14 17:37:08,081: INFO/MainProcess] mingle: searching for neighbors
[2026-02-14 17:37:09,096: INFO/MainProcess] mingle: all alone
[2026-02-14 17:37:09,104: INFO/MainProcess] celery@toughbook ready.
[2026-02-14 17:37:16,621: INFO/MainProcess] Task celery_task.tasks.recur_overdue_tasks.recur_overdue_tasks[2ae9314f-4ce3-4592-a776-2d586538fc9d] received
[2026-02-14 17:37:16,630: ERROR/ForkPoolWorker-8] Task celery_task.tasks.recur_overdue_tasks.recur_overdue_tasks[2ae9314f-4ce3-4592-a776-2d586538fc9d] raised unexpected: ModuleNotFoundError("No module named 'app'")
Traceback (most recent call last):
  File "/home/drill/.cache/pypoetry/virtualenvs/fastapi-server-LadTeqeZ-py3.11/lib/python3.11/site-packages/celery/app/trace.py", line 453, in trace_task
    R = retval = fun(*args, **kwargs)
                 ^^^^^^^^^^^^^^^^^^^^
  File "/home/drill/.cache/pypoetry/virtualenvs/fastapi-server-LadTeqeZ-py3.11/lib/python3.11/site-packages/celery/app/trace.py", line 736, in __protected_call__
    return self.run(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/drill/documents/dentec/Orbit/fastapi-server/celery_task/tasks/recur_overdue_tasks.py", line 153, in recur_overdue_tasks
    return loop.run_until_complete(_run())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/asyncio/base_events.py", line 654, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/home/drill/documents/dentec/Orbit/fastapi-server/celery_task/tasks/recur_overdue_tasks.py", line 38, in _run
    import app.container  # noqa: F401 β€” side-effect import
    ^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'app'```

And the _project_root variable is set as `/home/drill/documents/dentec/Orbit/fastapi-server`
limpid pollenBOT
#

@wooden coyote

Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.