vladimir.gorea
vladimir.gorea

Reputation: 661

ImproperlyConfigured Set the xxxx environment variable - django-environ

In my settings.py i'm using django-environ like this:

import os
import environ
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

env = environ.Env(
    SECRET_KEY=str,    
)

env_path = os.path.join(BASE_DIR, '.env')
environ.Env.read_env('.env')

SECRET_KEY = env('SECRET_KEY')

My .env file looks like this

SECRET_KEY = ******************

However, when I run the app i get

django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable

The .env file is found and lines are being read from it, so there's no problem with finding it, but somehow it doesn't work.

While following the execution thread i discovered that the regex for interpreting the .env lines is returning nothing

environ/environ.py

        for line in content.splitlines():
        m1 = re.match(r'\A(?:export )?([A-Za-z_0-9]+)=(.*)\Z', line)
        if m1:
            key, val = m1.group(1), m1.group(2)
            m2 = re.match(r"\A'(.*)'\Z", val)
            if m2:
                val = m2.group(1)
            m3 = re.match(r'\A"(.*)"\Z', val)
            if m3:
                val = re.sub(r'\\(.)', r'\1', m3.group(1))
            cls.ENVIRON.setdefault(key, str(val))enter code here

re.match(r'\A(?:export )?([A-Za-z_0-9]+)=(.*)\Z', line) is returning none

Am i missing something here?

Upvotes: 2

Views: 4014

Answers (1)

vladimir.gorea
vladimir.gorea

Reputation: 661

I found the answer by checking the regex responsible for interpreting the .env lines - \A(?:export )?([A-Za-z_0-9]+)=(.*)\Z

There should be no spaces between the key and the value

Problem:

KEY = VALUE

Good:

KEY=VALUE

Upvotes: 6

Related Questions