-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_dotenv.py
executable file
·50 lines (39 loc) · 1.24 KB
/
load_dotenv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/python3
import logging
import os
from pathlib import Path
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
try:
import click
except ImportError:
print("Please install click with pip3 install click")
exit(1)
def load_env_from_line(line: str):
line = line.strip()
if not line or line.startswith("#"):
return
key, value = line.split("=", 1)
key = key.strip()
value = value.strip()
os.environ[key] = value
def load_dotenv_from_file(env_file_path: Path):
with open(env_file_path, "r") as f:
for line in f:
load_env_from_line(line)
@click.command()
@click.option("--env-file", default=".env", help="Path to .env file")
def load_dotenv(env_file: str):
"""Load environment variables from .env file"""
env_file_path = Path(env_file)
if not env_file_path.exists():
logger.info(f"No .env file found at {str(env_file_path)}")
return
try:
load_dotenv_from_file(env_file_path=env_file_path)
logger.info("Loaded environment variables from .env file")
except Exception as e:
logger.error(f"Failed to load environment variables from .env file: {e}")
exit(1)
if __name__ == "__main__":
load_dotenv()