eventlet.monkey_patch() throws an exception: The SECRET_KEY setting must not be empty

649 views Asked by At

I have a Django project. The packages I use: Django 1.11, Eventlet 0.21.0. Python version is 3.4.3. There are the following configuration files:

1) The main project's configuration file settings.py. It's main role to import one of the other's two configuration files.

try:
    from .settings_local import *
except ImportError:
    from .conf.common import *

2) settings_local.py. The purpose of this file to import all the settings from common.py and override those inside the file's body (or add some new settings) for example for testing purposes:

from .conf.common import *
...

3) common.py. The file that contains all the configurations settings.

import eventlet
eventlet.monkey_patch(all=True, MySQLdb=True)
...

When I try to check configuration I get the following error:

./manage.py check

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/vagrant/.virtualenvs/<project_name>/lib/python3.4/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/home/vagrant/.virtualenvs/<project_name>/lib/python3.4/site-packages/django/core/management/__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/vagrant/.virtualenvs/<project_name>/lib/python3.4/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/vagrant/.virtualenvs/<project_name>/lib/python3.4/site-packages/django/core/management/base.py", line 322, in execute
    saved_locale = translation.get_language()
  File "/home/vagrant/.virtualenvs/<project_name>/lib/python3.4/site-packages/django/utils/translation/__init__.py", line 195, in get_language
    return _trans.get_language()
  File "/home/vagrant/.virtualenvs/<project_name>/lib/python3.4/site-packages/django/utils/translation/__init__.py", line 59, in __getattr__
    if settings.USE_I18N:
  File "/home/vagrant/.virtualenvs/<project_name>/lib/python3.4/site-packages/django/conf/__init__.py", line 56, in __getattr__
    self._setup(name)
  File "/home/vagrant/.virtualenvs/<project_name>/lib/python3.4/site-packages/django/conf/__init__.py", line 41, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/vagrant/.virtualenvs/<project_name>/lib/python3.4/site-packages/django/conf/__init__.py", line 110, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/home/vagrant/.virtualenvs/<project_name>/lib/python3.4/importlib/__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/vagrant/<project_name>/<project_name>/<project_name>/settings.py", line 2, in <module>
    from .settings_local import *
  File "/vagrant/<project_name>/<project_name>/<project_name>/settings_local.py", line 1, in <module>
    from .conf.common import *
  File "/vagrant/<project_name>/<project_name>/<project_name>/conf/common.py", line 3, in <module>
    eventlet.monkey_patch(all=True, MySQLdb=True)
  File "/home/vagrant/.virtualenvs/<project_name>/lib/python3.4/site-packages/eventlet/patcher.py", line 254, in monkey_patch
    _green_existing_locks()
  File "/home/vagrant/.virtualenvs/<project_name>/lib/python3.4/site-packages/eventlet/patcher.py", line 345, in _green_existing_locks
    if isinstance(obj, rlock_type):
  File "/home/vagrant/.virtualenvs/<project_name>/lib/python3.4/site-packages/django/utils/functional.py", line 238, in inner
    self._setup()
  File "/home/vagrant/.virtualenvs/<project_name>/lib/python3.4/site-packages/django/conf/__init__.py", line 41, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/vagrant/.virtualenvs/<project_name>/lib/python3.4/site-packages/django/conf/__init__.py", line 129, in __init__
    raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

I figured out that an exception initially occurs in /lib/python3.4/site-packages/eventlet/patcher.py in a function _green_existing_locks() and then propagates up to __init.py__.

Could someone tell me what am I doing wrong?

2

There are 2 answers

2
iklinac On

django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

Following error signifies that SECRET_KEY is not set in settings

0
temoto On

Please observe the difference between settings and code.

# settings.py
SECRET_KEY = 'abc...'
DEBUG = True
# just a list of constant definitions

# your common.py
eventlet.monkey_patch()
# it's code that changes python environment

# another bad example of settings.py with similar problem
open('/tmp/flag-file-1', 'w')
os.remove('/tmp/flag-file-2')

you don't put side effect generating code into settings. Find a place for initialisation code according to Django guidelines for your version and place monkey_patch() there.