Skip to content

Commit 4bf4ebd

Browse files
authored
Update README.md
1 parent 550eac7 commit 4bf4ebd

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

Day-05/README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,21 @@ Here's a comprehensive guide to working with environment variables in Python.
55

66
using below command we should export env variable to linux/mac
77
```
8+
# Add to ~/.bashrc, ~/.zshrc, or ~/.profile
9+
export API_KEY="your_api_key_here"
810
export DB_HOST="localhost"
911
```
1012

1113
In the code we call env variable by using `os.getenv`
1214
```
1315
import os
1416
15-
## ** Simple get with optional default **
16-
api_key = os.getenv('API_KEY', 'default-key')
17+
# Method 1: os.environ (raises KeyError if missing)
18+
api_key = os.environ['API_KEY']
19+
20+
# Method 2: os.getenv() (safer, returns None if missing)
21+
db_host = os.getenv('DB_HOST')
22+
23+
# Method 3: With default value
24+
db_port = os.getenv('DB_PORT', '5432') # Default to 5432 if not set
1725
```

0 commit comments

Comments
 (0)