Set up tokens and use as environment variables
When you are working on your local development computer and if the secret doesn't change between executions, you can set the secrets in a special file.
Secrets can be about databases, passwords, API gateways, operating modes that determine whether our app is running in developer or production mode. Basically, all the code you need to actually access the different functions of your app.
Those tokens, secrets, passwords might not necessarily be things which you want your user to be able to see.
In this tutorial you will see how to use a .env
file to make it easier to share with teammates while allowing them to set their own environmental variables, but not necessarily check in and let the whole world see.
Tokens are in the clear
This demonstrates how to store the tokens in the clear.
Set .gitignore
When you build out a project in Git, you will have a file named .gitignore
. Open it to be sure that the list of files Git ignores on check in include. You should see,
Create the .env file
To configure the development environment, add a .env in the root directory of your project:
For example, use:
To test, run:
To append an new environment variable:
Make the token file available only to you
Secure the token file so that only the user can see it.
chmod 600
is a file permission setting in Linux that grants read and write permissions to the owner, while denying all permissions to the group and other users.
Use the token in a command line
You can load the file and set the token to an environment variable.
To see the environment variables:
[!NOTE] If you want to encrypt your variables as passwords, use encpass.sh
Use the keys in your Jupyter Notebook
To use the keys in a notebook in three steps:
- Install
dotenv
into your Python environment. - Load the environment in your .env file and assign variables in your application
Let's start with installing dotevn
Install dotenv
in your Python environment
You can install using pip or conda.
Load the environment in your .env file and assign variables in your application
At the top of your notebook:
from dotenv, import load_dotenv
def configure() -> None:
"""Load environment variables and configure OpenAI API key."""
load_dotenv()
music_secret = os.getenv("MUSICSECRET")
music_preference = os.getenv("MUSICPREFEENCE")
openai.api_key = os.getenv("OPENAI_API_KEY")
With load_dotenv(override=True)
or dotenv_values()
, the value of a variable is the first of the values defined in the following list:
- Value of that variable in the
.env
file. - Value of that variable in the environment.
- Default value, if provided. 4/ Empty string.
With load_dotenv(override=False)
, the value of a variable is the first of the values defined in the following list:
- Value of that variable in the environment.
- Value of that variable in the .env file.
- Default value, if provided.
- Empty string.
To test:
configure()
print("MUSICSECRET = ", music_secret)
print("MUSICPREFEENCE = ", music_preference)
print("OPENAI_API_KEY = ", openai.api_key)
Another technique
You can also use magic commands to load your .env
data.
For more information and techniques
See python-dotenv