Ubuntu 18.04下用Systemd配置Celery遇模块路径错误
Let's tackle this path issue head-on—since you know it's a path problem, we just need to make sure Celery can see your ph Django module correctly. Here's how to fix it:
1. Correct Your systemd Service Configuration
The root issue is that systemd isn't setting the right working directory or Python path for Celery to locate your ph module. Create or edit your Celery systemd service file (usually at /etc/systemd/system/celery.service) with these critical settings:
[Unit] Description=Celery Service After=network.target [Service] Type=forking User=username Group=username # Set the working directory to the parent of your `ph` module (the src folder) WorkingDirectory=/home/username/projects/src # Add your src folder to Python's path so it can find the `ph` module Environment="PYTHONPATH=/home/username/projects/src" # Use your virtual environment's Celery binary, and specify the correct app module ExecStart=/home/username/env/prod/bin/celery multi start w1 w2 \ --app=ph.celery \ --logfile=/var/log/celery/%n.log \ --pidfile=/var/run/celery/%n.pid ExecStop=/home/username/env/prod/bin/celery multi stopwait w1 w2 \ --pidfile=/var/run/celery/%n.pid ExecReload=/home/username/env/prod/bin/celery multi restart w1 w2 \ --app=ph.celery \ --logfile=/var/log/celery/%n.log \ --pidfile=/var/run/celery/%n.pid Restart=always [Install] WantedBy=multi-user.target
Key Notes Here:
WorkingDirectorypoints to/home/username/projects/srcbecause yourphmodule lives directly inside this folder—Celery needs to start here to find the module.PYTHONPATHexplicitly adds the src folder to Python's module search path, eliminating any ambiguity.--app=ph.celerytells Celery to use theceleryapp instance defined inph/celery.py(make sure this file exists and is configured correctly).
2. Verify Your Celery App Setup in Django
Double-check your ph/celery.py file to ensure it's properly hooked into Django's settings:
import os from celery import Celery # Set the default Django settings module for the 'celery' program. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ph.settings') app = Celery('ph') # Configure Celery using Django settings (prefix with CELERY_) app.config_from_object('django.conf:settings', namespace='CELERY') # Auto-discover tasks from all registered Django apps app.autodiscover_tasks()
This ensures Celery knows where your Django project's settings are, and can load your tasks correctly.
3. Apply the Changes and Test
Once you've updated the service file and confirmed your Celery setup:
- Create the log and pid directories if they don't exist:
sudo mkdir -p /var/log/celery /var/run/celery sudo chown username:username /var/log/celery /var/run/celery - Reload systemd to pick up the new service config:
sudo systemctl daemon-reload - Restart the Celery service:
sudo systemctl restart celery - Check the logs to confirm the error is gone:
journalctl -u celery -f
If everything is set up correctly, you shouldn't see the ModuleNotFoundError anymore. The key was making sure both the working directory and Python path point to the right location where your ph module lives.
内容的提问来源于stack exchange,提问作者user3541631

