You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RQ工作者异常:ImportError: No module named XXX及配置问题咨询

解决RQ Worker抛出的ImportError问题

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 of worker.py or clock.py to 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 the project/ subfolder. Navigate to your project's root (the folder that holds app/, project/, and controllers/) and run:

    rq worker -c project.worker
    

    This ensures the Worker can resolve imports correctly from the root context.

  • Use absolute imports instead of relative ones
    In clock.py, make sure you're importing modules using absolute paths (relative to the project root). For example:

    from controllers.leads_fb import your_target_function
    

    Relative imports (like from .controllers...) can break in RQ's isolated worker context.

  • Double-check your __init__.py files
    Ensure both project/ and controllers/ have valid __init__.py files (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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:44:18