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

使用Gunicorn启动Django时遇ImportError: No module named 'environ'求助

Troubleshooting "Cannot Import environ" with Gunicorn on DigitalOcean (Ubuntu 14.04 + Django 2.0.5 + Cookiecutter)

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 runserver uses:

    which python
    

    This 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 gunicorn
    

    If 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:8000
    

    Or 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:
    import environ
    
    (Not from django.conf import environ or 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 .env file path in your settings is correct. Cookiecutter typically uses:
    env = environ.Env()
    env.read_env(str(BASE_DIR / ".env"))
    
    If Gunicorn can't find this .env file (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 --chdir flag 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 .env if you use it)
  • Set appropriate permissions for the .env file (avoid overly open permissions, 644 is 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.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 03:43:08