diff --git a/docs/tech_docs/Python-Virtual-Environments.md b/docs/tech_docs/Python-Virtual-Environments.md index 74267c0..4ef3504 100644 --- a/docs/tech_docs/Python-Virtual-Environments.md +++ b/docs/tech_docs/Python-Virtual-Environments.md @@ -217,4 +217,66 @@ Organizing virtual environments is crucial for maintaining a clean and efficient These guidelines aim to provide a structured approach to managing Python virtual environments, enhancing clarity and efficiency in your development workflow. +--- + +# Managing Environment Variables in Python Virtual Environments + +Using `.env` files for environment-specific settings is a best practice in Python development. This guide explains how to set up and use `.env` files within virtual environments. + +## What are `.env` Files? + +- `.env` files are simple text files that contain environment variables. +- They are used to store configuration settings that should not be hard-coded in your code, such as API keys, database URLs, and other sensitive information. + +## Setting Up `.env` Files + +### 1. Creating `.env` File + +- Place a `.env` file in your project's root directory. +- Add environment variables in the format `KEY=value`. + + \```plaintext + # Example .env file + DATABASE_URL=postgresql://user:password@localhost/mydatabase + API_KEY=yourapikey + \``` + +### 2. Using `python-dotenv` to Load Variables + +- Install `python-dotenv` to easily load the variables from `.env` file. + + \```bash + pip install python-dotenv + \``` + +- Import `dotenv` in your main script and load the variables. + + \```python + from dotenv import load_dotenv + load_dotenv() + \``` + +## Accessing Environment Variables + +- Access variables using `os.environ`. + + \```python + import os + database_url = os.getenv('DATABASE_URL') + api_key = os.getenv('API_KEY') + \``` + +## Best Practices + +- **Never Commit `.env` Files**: Add `.env` to your `.gitignore` file to prevent sensitive information from being committed to version control. +- **Use Different `.env` Files for Different Environments**: For example, `.env.development`, `.env.production` for different deployment stages. +- **Keep `.env` File Updated**: Regularly update the `.env` file with any new or changed environment variables. + +## Security Considerations + +- Keep your `.env` files secure and only share them with trusted team members. +- Regularly audit the environment variables and remove any that are no longer in use. + +By following these practices, you can securely manage environment-specific settings in your Python projects, keeping sensitive information out of your source code. + --- \ No newline at end of file