Setting Environment Variables
Nice people from the Internet wrote here about how to use dotenv to pass around credentials that you want to keep private: https://help.pythonanywhere.com/pages/environment-variables-for-web-apps/. So I feel that it make sense and for different projects from now on I used python dotenv.
Set of steps to follow
- create venv in your project directory
python -m venv venv && venv\Scripts\activate.bat
- Install python-dotenv into your virtualenv:
pip install python-dotenv
- Set my
PRIVATE_VARIABLE='test'
:vim .env
- Inside .env add a variable and assign the value 'test' to it:
# myapp settings
PRIVATE_VARIABLE='test'
We populated this PRIVATE_VARIABLE
with test
which I consider to keep secret.
This will be an environment variable to pass around within venv
- The variable inside
.dotenv
retrieved in app.py as follows:
from dotenv import dotenv_values
DOTENV_CONFIG = dotenv_values(".env") # access dict object {"PRIVATE_VARIABLE": "test"}
API_URL = DOTENV_CONFIG['AWS_ENDPOINT']
Additional step to fix variable "privacy"
- I want to keep it away from others but available for python within this particular venv.
I want to add .env to my .gitignore. This way when I post my code to Github I keep
.env
private (away from vcs). In.gitignore
I add the following:
.env