使用Gunicorn启动Django时遇ImportError: No module named 'environ'求助
Hey there, let's dig into this ImportError: cannot import environ issue you're hitting with Gunicorn. Since python manage.py runserver works perfectly, the problem almost always boils down to environment mismatches between your local runserver context and how Gunicorn is executing your app. Here are the most likely fixes to try:
1. Verify Gunicorn is using your virtual environment
It's super common for Gunicorn to default to the system Python instead of your project's virtual environment, where you installed django-environ and other dependencies.
First, check which Python your
runserveruses:which pythonThis should point to your virtual environment's Python (e.g.,
/home/your_user/venv/bin/python).Now check which Python Gunicorn is tied to:
which gunicornIf it points to
/usr/bin/gunicorn(system Python), run Gunicorn directly using your virtual environment's Python to ensure it uses the right dependencies:/path/to/your/venv/bin/python -m gunicorn config.wsgi:application --bind 0.0.0.0:8000Or activate your virtual environment first before running Gunicorn:
source /path/to/your/venv/bin/activate gunicorn config.wsgi:application --bind 0.0.0.0:8000
2. Confirm django-environ exists in Gunicorn's environment
Double-check that the package is installed in the same environment Gunicorn uses. Run this command with your virtual environment's Python:
/path/to/your/venv/bin/python -c "import environ; print(environ.__version__)"
If this throws an error, reinstall django-environ directly in that environment:
/path/to/your/venv/bin/pip install django-environ==0.4.4
3. Validate your Cookiecutter Django configuration
Cookiecutter Django has specific setup patterns for environment variables—make sure your config files are correctly referencing django-environ:
- In
config/settings/base.py, confirm the import line is:
(Notimport environfrom django.conf import environor any other incorrect variation.) - Ensure your WSGI file (
config/wsgi.py) loads the correct settings module. For production, it should look like:os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.production") - Also, verify the
.envfile path in your settings is correct. Cookiecutter typically uses:
If Gunicorn can't find thisenv = environ.Env() env.read_env(str(BASE_DIR / ".env")).envfile (e.g., if you're running from the wrong directory), it might trigger import issues indirectly.
4. Check Gunicorn's working directory
If you run Gunicorn from a directory other than your project root (where manage.py lives), it might fail to locate your settings or dependencies. Either:
- Navigate to your project root before running Gunicorn, or
- Use the
--chdirflag to specify the root directory:gunicorn --chdir /path/to/your/project_root config.wsgi:application --bind 0.0.0.0:8000
5. Rule out permission issues (DigitalOcean-specific)
If you're running Gunicorn as a system service (e.g., with systemd), ensure the service user (like www-data) has access to:
- Your virtual environment directory
- Your project files (especially
.envif you use it) - Set appropriate permissions for the
.envfile (avoid overly open permissions,644is safe):chmod 644 /path/to/your/project_root/.env
If using systemd, update your service file to explicitly set the environment and working directory:
[Service] User=www-data Group=www-data WorkingDirectory=/path/to/your/project_root Environment="PATH=/path/to/your/venv/bin" ExecStart=/path/to/your/venv/bin/gunicorn --workers 3 config.wsgi:application --bind 0.0.0.0:8000
Start with the first two steps—virtual environment mismatches are the most frequent culprit here. Let me know if any of these fix the issue!
内容的提问来源于stack exchange,提问作者Dominic M.

