Update docs/tech_docs/Python-Virtual-Environments.md
This commit is contained in:
@@ -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.
|
||||
- Add environment variables in the format `KEY=value`.
|
||||
|
||||
\```plaintext
|
||||
```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
|
||||
```bash
|
||||
pip install python-dotenv
|
||||
\```
|
||||
```
|
||||
|
||||
- Import `dotenv` in your main script and load the variables.
|
||||
|
||||
\```python
|
||||
```python
|
||||
from dotenv import load_dotenv
|
||||
load_dotenv()
|
||||
\```
|
||||
```
|
||||
|
||||
## Accessing Environment Variables
|
||||
|
||||
- Access variables using `os.environ`.
|
||||
|
||||
\```python
|
||||
```python
|
||||
import os
|
||||
database_url = os.getenv('DATABASE_URL')
|
||||
api_key = os.getenv('API_KEY')
|
||||
\```
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
|
||||
Reference in New Issue
Block a user