0% found this document useful (0 votes)
9 views2 pages

Env and GPT Api

The load_dotenv function loads environment variables from a .env file into a Python application, making them accessible via os.getenv(). To set it up, install the python-dotenv package, create a .env file with your variables, and import load_dotenv in your script to load these variables. Finally, run your script to access and print the values of the environment variables.

Uploaded by

aegr82
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Env and GPT Api

The load_dotenv function loads environment variables from a .env file into a Python application, making them accessible via os.getenv(). To set it up, install the python-dotenv package, create a .env file with your variables, and import load_dotenv in your script to load these variables. Finally, run your script to access and print the values of the environment variables.

Uploaded by

aegr82
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

help understanding how the load_dotenv code works.

step-by-step instructions on
how you can create and setup a .env file on my own computer.
The load_dotenv function is used to load environment variables from a .env file
into your Python application. Here's a simple explanation and step-by-step
instructions on setting it up:

What Does load_dotenv Do?


1. Environment Variables: These are variables in your operating system that
scripts and programs can access. They're useful for storing sensitive
information like API keys.
2. .env File: This is a file where you can set your environment variables so
that they can be easily loaded into your application.
3. load_dotenv Function: This function reads the .env file and loads the
environment variables into your system environment, making them
accessible in your Python code using os.getenv() or similar.

Step-by-Step Instructions

1. Install python-dotenv :

 Open your terminal or command prompt.


 Run the following command to install the package:

Copiar código

pip install python-dotenv

2. Create a .env File:

 Navigate to your project directory.


 Create a new file named .env (make sure to include the dot at the
beginning).
 Open this file with a text editor and add your environment variables.
For example:

Copiar código

API_KEY=your_api_key_here
SECRET_KEY=your_secret_key_here
3. Load Environment Variables in Your Python Script:

 In your Python file, import load_dotenv and os :

python

Copiar código

from dotenv import load_dotenv


import os

# Load the .env file


load_dotenv()

# Access the environment variables


api_key = os.getenv('API_KEY')
secret_key = os.getenv('SECRET_KEY')

print(f"API Key: {api_key}")


print(f"Secret Key: {secret_key}")

4. Run Your Script:

 Execute your Python script. It should print the values of your


environment variables stored in the .env file.

This setup helps you manage sensitive information securely and keeps it out of
your source code. Let me know if you need any more help!

You might also like