Update docs/tech_docs/Python-Virtual-Environments.md

This commit is contained in:
2024-01-09 07:00:32 +00:00
parent 4d434d56e8
commit 154b9437c9

View File

@@ -235,36 +235,36 @@ Using `.env` files for environment-specific settings is a best practice in Pytho
- Place a `.env` file in your project's root directory. - Place a `.env` file in your project's root directory.
- Add environment variables in the format `KEY=value`. - Add environment variables in the format `KEY=value`.
\```plaintext ```plaintext
# Example .env file # Example .env file
DATABASE_URL=postgresql://user:password@localhost/mydatabase DATABASE_URL=postgresql://user:password@localhost/mydatabase
API_KEY=yourapikey API_KEY=yourapikey
\``` ```
### 2. Using `python-dotenv` to Load Variables ### 2. Using `python-dotenv` to Load Variables
- Install `python-dotenv` to easily load the variables from `.env` file. - Install `python-dotenv` to easily load the variables from `.env` file.
\```bash ```bash
pip install python-dotenv pip install python-dotenv
\``` ```
- Import `dotenv` in your main script and load the variables. - Import `dotenv` in your main script and load the variables.
\```python ```python
from dotenv import load_dotenv from dotenv import load_dotenv
load_dotenv() load_dotenv()
\``` ```
## Accessing Environment Variables ## Accessing Environment Variables
- Access variables using `os.environ`. - Access variables using `os.environ`.
\```python ```python
import os import os
database_url = os.getenv('DATABASE_URL') database_url = os.getenv('DATABASE_URL')
api_key = os.getenv('API_KEY') api_key = os.getenv('API_KEY')
\``` ```
## Best Practices ## Best Practices