Django settings SECRET_KEY
Question: I have the following structure of my project  

    
    
    project
    --project
    ----settings
    ------base.py
    ------development.py
    ------testing.py
    ------secrets.json
    --functional_tests
    --manage.py
    

**development.py** and **testing.py** 'inherit' from **base.py**

    
    
    from .base import *
    

So, where I have problems

I have the SECRET_KEY for Django in secrets.json, which is stored in
**settings** folder

I load this key like this (saw this in "Two scoops of Django")

    
    
    import json
    from django.core.exceptions import ImproperlyConfigured
    
    key = "secrets.json"
    with open(key) as f:
        secrets = json.loads(f.read())
    
    def get_secret(setting, secret=secrets):
        try:
            return secrets[setting]
        except KeyError:
            error_msg = "Set the {} environment variable".format(setting)
            raise ImproperlyConfigured(error_msg)
    
    SECRET_KEY = get_secret("SECRET_KEY")
    

But when I run `python manage.py runserver`

> Blah-blah-blah  
>  django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must
> not be empty.

After some investigations I got the following

  1. If I put `print(os.getcwd())` inside **base.py** I get `/media/grimel/Home/project/` instead of `/media/grimel/Home/project/project/settings/`
  2. This code works only if I replace:  
`key = "secrets.json"`  
by  
`key = "project/settings/secrets.json"`

Personally, I don't like this solution.  
So, questions:

  1. Why, for base.py current working directory is so confusing?
  2. What's a better approach in solving this problem?


Answer: The working directory is based on how you run the program, in your case
`python manage.py runserver` hints that your working directory is the one
containing `manage.py`. Beware that this can vary when run as WSGI script or
otherwise, so your concern with using `key = "project/settings/secrets.json"`
is valid.

One solution is to use the value of `__file__` in `base.py`, likely to be
`"project/settings/base.py"`. I would use something like

    
    
    import os
    BASE_DIR = os.path.dirname(__file__)
    key = os.path.join(BASE_DIR, "secrets.json")
    

