Skip to content

Credential Management using .env

The .env file is a simple and effective way to manage credentials and other environment-specific settings. Here's how you can use it:

🌐 Store proxy details

If you're working behind a proxy, you can store your proxy information in the .env file. These will be automatically loaded and utilized by Pipenv during package installation. Here's the format:

HTTP_PROXY='http://user:password@your-proxy-url:port'
HTTPS_PROXY='https://user:password@your-proxy-url:port'

Replace user, password, your-proxy-url, and port with your actual proxy details.

🗝️ Store credentials/secrets/keys

You can also store API keys and other credentials in the .env file. Here's an example of how to store an API key:

OPENAI_API_KEY='xxxx-xxxx'

Replace xxxx-xxxx with your actual OpenAI API key.

👨‍🔧 Using .env variables inside Python code

To utilize the variables defined in the .env file into your Python code, we recommend using the python-decouple library. This library simplifies the process of separating settings from your source code, making it easier to change these settings without modifying the code itself.

Here's an example of how you can use python-decouple to access the credentials stored in your .env file:

from decouple import config

# Get the OpenAI API key
openai_api_key = config('OPENAI_API_KEY')

# Get the HTTP proxy
http_proxy = config('HTTP_PROXY')

# Get the HTTPS proxy
https_proxy = config('HTTPS_PROXY')

In this code, config('OPENAI_API_KEY'), config('HTTP_PROXY'), and config('HTTPS_PROXY') retrieve the values of OPENAI_API_KEY, HTTP_PROXY, and HTTPS_PROXY from the .env file, respectively.

Note

The .env file does not get committed to your version control system. It typically contains sensitive information that should not be shared publicly.