How do I use variables created in venv bash in my django app?
Question: If I create a variable in my venv like this

    
    
    DJANGO_SERVER_TYPE=local
    

and on heroku I have

    
    
    DJANGO_SERVER_TYPE=production
    

why won't my system recognize this

    
    
    from .base import *
    import os
    if os.environ['DJANGO_SERVER_TYPE'] == 'local':
        try:
            from .local import *
        except:
            pass
    
    if os.environ['DJANGO_SERVER_TYPE'] == 'production':
        try:
            from .production import *
        except:
            pass
    

I keep getting this error

    
    
    File "/Users/ray/Desktop/myheroku/practice/src/gettingstarted/settings/__init__.py", line 3, in <module>
        if os.environ['DJANGO_SERVER_TYPE'] == 'local':
        File "/Users/ray/Desktop/myheroku/practice/bin/../lib/python3.5/os.py", line 683, in __getitem__
        raise KeyError(key) from None
        KeyError: 'DJANGO_SERVER_TYPE'
    

How do I use the local variable that I created?


Answer: The issue is that your environment variable does NOT exist locally. There are
a lot of ways to set environment variables locally.

I highly recommend using the
[autoenv](https://github.com/kennethreitz/autoenv) written by Kenneth Reitz.
This is a cool tool you can install locally on your computer. See [setting an
environment variable in
virtualenv](https://stackoverflow.com/questions/9554087/setting-an-
environment-variable-in-virtualenv) for more information.

The way it works is that you define a file called `.env` in your project
directory, and list all of your environment variables inside of it. For
instance:

    
    
    export DJANGO_SERVER_TYPE=local
    

Everytime you enter that directory on the command line (like `cd
~/my_project`), autoenv will automatically set whatever environment variables
you've got in that folder as active. It's very useful.

Anyhow, wherever you're currently storing your environment variables, you'll
need to `export` them if you don't want to use something like autoenv.

So, let's say you've got a file (`env.sh`) that has your environment variables
defined in it like so:

    
    
    DJANGO_SERVER_TYPE=local
    

You can 'run' this file by saying `sh env.sh`. Now, you might expect that this
would run, and you would now have `DJANGO_SERVER_TYPE` defined in your
environment variables, right?

Well, not exactly.

In `sh` (the scripting language you're actually using here), you need to put
the `export` keyword in front of a variable if you want the variable to be
exported OUTSIDE of the running script.

So, in order to actually set your variable, you would have to say:

    
    
    export DJANGO_SERVER_TYPE=local
    

Then run that file as a script: `sh env.sh`. If you do that, you'll notice
that now your environment variable IS actually set.

Another useful thing to know while testing this out, is that if you run the
`env` command on the command line, it will output a list of all environment
variables currently set LOCALLY. This is useful for debugging, eg:

    
    
    env | grep DJANGO_
    

