1

I am trying to set environment variables in my Python 3 virtual environment, by setting them in the activate file. (I am setting them for my development environment in this case.)

Having activated the virtualenv (env/scripts/activate), seeing "(venv)" at the beginning of my powershell lines, I run:

python manage.py runserver

In the manage.py file I have specified the settings file to use. And the content of that settings file I will show down below in this post.

The variables are meant to be passed to my Django application, but I get the following error:

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

Searching the internet, and mainly SO, I find different approaches to setting environment variables, but each gives the same error. So I wonder what I am doing wrong.

What I have tried:

It seems I have to set the environment variables in the 'activate.bat' file located at env/scripts/activate.bat. I have tried different ways of doing this, and I have tried the same ways in this file: env/scripts/activate (without the .bat extension), just to be sure.

I have tried adding lines of the formats found below, to the end of the activate files:

set "SECRET_KEY=^af*$gsdgaGJA4gg($g134FADGJ"

set SECRET_KEY=^af*$gsdgaGJA4gg($g134FADGJ

export SECRET_KEY="^af*$gsdgaGJA4gg($g134FADGJ"

(I have not actually used the secret key as found above, but one generated by Django itself, this one is for demonstration purposes only and doesn't meet Django's requirements.)

My settings.py file

In my settings.py file I have this line to get the SECRET_KEY:

SECRET_KEY = os.environ['SECRET_KEY']

I have decided not to post the full settings.py file since it seems unnecessary.

gogaz
  • 2,323
  • 2
  • 23
  • 31
Rik Schoonbeek
  • 3,908
  • 2
  • 25
  • 43
  • Are you aware that what you tried is not taken into account by your shell unless you execute `env/scripts/activate.bat` ? – gogaz Aug 20 '18 at 14:30
  • Use the `postactivate` hook offered by virtualenvwrapper. Seems a duplicate of https://stackoverflow.com/questions/9554087/setting-an-environment-variable-in-virtualenv – dirkgroten Aug 20 '18 at 17:26
  • I'm not using virtualenvwrapper, but I will try that – Rik Schoonbeek Aug 21 '18 at 12:50
  • using set "SECRET_KEY=^af*$gsdgaGJA4gg($g134FADGJ" instead of set SECRET_KEY = ' ... ' and os.environ.get("SECRET_KEY") fixed the issue on my end – tienow Jun 08 '22 at 21:13

5 Answers5

0

SECRET_KEY has to be included in your settings.py.

It can be taken from env variable but you have to handle it by yourself.

By handling it by yourself I mean adding to settings.py sth like:

SECRET_KEY = os.environ.get('SECRET_KEY', '')
Kamil
  • 1,256
  • 10
  • 17
  • 1
    I did, I somehow cut out the last part of my post while editing it. I added it again. Thanks for the quick reply though. – Rik Schoonbeek Aug 20 '18 at 14:28
  • Could you try to go into `python manage.py shell` and run: `from django.conf import settings; print(settings.SECRET_KEY)`. Is it empty? Also, you could try setting env variable per command, like `SECRET_KEY='xxx' python manage.py runserver`. – Kamil Aug 20 '18 at 14:30
0

I was having this same issue and I fixed it by closing my shell and then re-activating my virtual env after my environment variables were added.

If you open shell and run the python interpreter, you can look at your environment variables by running Import os and os.environ.keys(). If you don't see your variables, chances are you need to restart your virtual environment so that it can pick up the system variables you added.

0

I see no answer marked then I add mine in case it can help people.

I think here you had the good configuration but you forgot to close cmd prompt or powershell prompt after adding your virtual environment variable. And start a new cmd prompt in order to force reload environement variables.

For people under an IDE like Visual Studio Code, it's the same, you need to close all VSCode windows and open again VSCode.

To summup the following configuration is good:

In settings.py:

SECRET_KEY = os.environ['SECRET_KEY']

Add an environnement variable SECRET_KEY for your windows user.

Nico
  • 3,430
  • 4
  • 20
  • 27
0

I encountered this problem because I was missing a critical environment variable. The error wasn't very informative.

BITWISE
  • 115
  • 1
  • 8
0

With me, Powershell is the issue. None of the variables set in activate.bat are available in my session, but if I run through a CMD terminal, the variables are set.

Kaspur
  • 1
  • 1