Skip to content

Commit 155ee4c

Browse files
committed
Add dotenv example
1 parent cfb53aa commit 155ee4c

5 files changed

Lines changed: 55 additions & 4 deletions

File tree

source-code/enviroment-variables/README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
# Environment variables
22

3-
This directory shows some examples of how to use environment variables in a Python
4-
script.
3+
This directory shows some examples of how to use environment variables in a
4+
Python script.
5+
56

67
## What is it?
78

8-
1. `accumulate_data.py`: script that will read all `.txt` files in a directory defined
9-
in an environment variable `DATA_DIR`.
9+
1. `accumulate_data.py`: script that will read all `.txt` files in a directory
10+
defined in an environment variable `DATA_DIR`.
1011
1. `data`: directory that contains two text files with data.
12+
1. `dotenv`: the `dotenv` package is used to load environment variables from a
13+
`.env` file.
14+
1115

1216
## How to use it?
1317

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# include .env file to prevent sensitive data from being committed
2+
.env
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# dotenv
2+
3+
The `dotenv` package is a simple and efficient way to load environment
4+
variables from a `.env` file into your application's environment. It is
5+
commonly used in development and testing environments to manage configuration
6+
settings without hardcoding them into the source code. Another important use
7+
case is to keep sensitive information, such as API keys and database
8+
credentials, out of version control systems.
9+
10+
11+
## What is it?
12+
13+
1. `dot_env`: file to be renamed to `.env` in the directory where the script is
14+
run.
15+
1. `dotenv_demo.py`: demo script that uses `dotenv` to load environment
16+
variables from the `.env` file.
17+
1. `.gitignore`: to ignore the `.env` file in version control.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
MY_SECRET="if you see this, expect black helicopters"
2+
MY_PATH="somewhere/to/go"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python3
2+
3+
from dotenv import load_dotenv
4+
import os
5+
6+
7+
def main():
8+
# Check whether the environment variable is already set
9+
if 'MY_SECRET' in os.environ:
10+
print('MY_SECRET is already set in the environment.')
11+
else:
12+
# Load environment variables from .env file
13+
print('Loading environment variables from .env file...')
14+
load_dotenv()
15+
16+
# Access an environment variable
17+
secret_key = os.getenv('MY_SECRET')
18+
19+
if secret_key:
20+
print(f'MY_SECRET_KEY: {secret_key}')
21+
else:
22+
print('MY_SECRET not found in environment variables.')
23+
24+
25+
if __name__ == '__main__':
26+
main()

0 commit comments

Comments
 (0)