Company
Date Published
Author
Julian Sequeira
Word count
606
Language
English
Hacker News points
None

Summary

The author expresses frustration with the conventional methods of handling environment variables in Python, emphasizing the inconvenience of having to redeclare them each time a terminal session is initiated. To address this, they introduce the use of the python-dotenv module, which allows environment variables to be stored in a ".env" file within a Python project directory, including virtual environments (venvs). By importing the load_dotenv function from python-dotenv at the start of a script, the environment variables specified in the ".env" file become accessible, ensuring persistence even when the venv is deactivated and reactivated. The author demonstrates how to implement this by creating a ".env" file, importing load_dotenv, and using os.getenv to access the variables within a script, ultimately offering a more efficient and time-saving solution. Additionally, they advise using a ".env.example" file to guide others on which variables need configuring if the code is shared publicly, while cautioning against pushing actual ".env" files to public repositories.