RQ工作者异常:ImportError: No module named XXX及配置问题咨询
Hey there, let's fix that RQ ImportError you're facing. First, let's recap your setup to make sure we're aligned:
Your project structure looks like this:
app/ run.py project/ __init__.py clock.py views.py worker.py controllers/ __init__.py leads_fb.py
You're running the clock module on app startup, which uses APScheduler to enqueue jobs into RQ, but you're hitting the error: RQ - ImportError: No module named XXX.
Here are the most common fixes for this issue:
Add your project root to Python's path
RQ Workers often can't find custom modules because the project root isn't in their Python path. Add this snippet at the top ofworker.pyorclock.pyto fix it:import sys import os # Get the path to the directory containing project/ and controllers/ project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(project_root)Launch the Worker from your project root directory
Don't start the RQ Worker inside theproject/subfolder. Navigate to your project's root (the folder that holdsapp/,project/, andcontrollers/) and run:rq worker -c project.workerThis ensures the Worker can resolve imports correctly from the root context.
Use absolute imports instead of relative ones
Inclock.py, make sure you're importing modules using absolute paths (relative to the project root). For example:from controllers.leads_fb import your_target_functionRelative imports (like
from .controllers...) can break in RQ's isolated worker context.Double-check your
__init__.pyfiles
Ensure bothproject/andcontrollers/have valid__init__.pyfiles (you already have them, which is good!)—these files tell Python to treat the directories as importable modules. Avoid putting broken code in these files, as that can also cause import errors.
内容的提问来源于stack exchange,提问作者mateocam

