Search code examples
pythonyoutube-apiyoutube-data-api.env

When I edit a .env file, and try to use the new variable the old one gets loaded


This is the content of .env file

YOUTUBE_API_KEY=SyCnieNj
# old-api-key
# YOUTUBE_API_KEY=AIzayCno

When i try to use YOUTUBE_API_KEY the old api is loaded This is the part of the python code:

import os
import json
import time
import googleapiclient.discovery
import googleapiclient.errors
from dotenv import load_dotenv

# Load API keys
load_dotenv()

# YouTube API setup
YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY")

Even after removing all the comment line from .env file os.getenv("YOUTUBE_API_KEY") is still loading old api key

Can anyone explain the issue here?

When I changed the api key in .env and reloaded the script the script is still using the old api key


Solution

  • I think the load_dotenv function fetches the environment variable's cached value. It can be resolved by enabling an override prop in the load_dotenv function.

    using this you can fetch the correct value whenever it is updated in the correct order, I think this will help(taken from the docs of python dotenv - https://pypi.org/project/python-dotenv/) -

    enter image description here

    load_dotenv(override=True)