#๐Ÿ”’ KeyError

58 messages ยท Page 1 of 1 (latest)

mortal cove
#

Hi, I am trying to pull values from a .env file but I get KeyError: 'CLIENT_ID'.
In my .env file I Have the value: CLIENT_ID = x.
My code is as follows:

from os import environ as env
from dotenv import load_dotenv

load_dotenv()

print('CLIENT_ID:  {}'.format(env['CLIENT_ID']))

Can someone help please? Thanks

gaunt needleBOT
#

@mortal cove

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

spice ginkgo
#

are you sure the env file exist and properly save with the correct data?

mortal cove
spice ginkgo
#

have you try print env

haughty hare
#

it might be a cwd issue. so it might not look for the file in the correct location

mortal cove
haughty hare
#

the environment contains not only your own variables. it's normal to have lots of other stuff

haughty hare
mortal cove
haughty hare
#

cwd depends on the folder you run it from, but what that is ultimately depends on the way you run your python file... from terminal? from IDE? some other way?

mortal cove
haughty hare
#

the directory you're inside of, is typically your cwd (current working directory)

#

and I'm somewhat sure python-dotenv looks in that folder for your .env file

mortal cove
haughty hare
#

yea, but the directory where your python file is, is not necessarily your current working directory

#

can you show the terminal and the comand you use to run your python script?

haughty hare
#

and the .env file is also in the NarReddit-Script-main folder?

haughty hare
#

have you double ckecked the file is named correctly? you can use the "dir" command in your terminal

#

yea it's .env not env

#

it needs to start with a dot

#

(that's why the lib is called dotenv)

mortal cove
#

and when I change the code to print(env) It's still not the output I'm expecting

haughty hare
#

what did you change the filename to?

mortal cove
white tide
#

just use override=True parameter in load_dotenv function

#

load_dotenv(override=True)

haughty hare
#

that's not gonna fix anything

white tide
#

it does

#

sometimes environment variables are stored for future use

#

overriding it helps when you have replaced it with new values

mortal cove
#

It didn't work

haughty hare
#

that doesn't explain the keyerror

white tide
#

hmm weird

haughty hare
#

the var doesn't exist, and you shouldn't use override anyway, as actually specified env vars should have priority... the .env is just a fallback

#

anyway

#

so the file is named literally ".env" - only those 4 characters? (just making sure)

#

(".env" is not an extension, it's the full filename) ๐Ÿ™‚

mortal cove
#

how can such a simple thing be so complicated haha

haughty hare
#

well , I have a feeling it's actually .env.txt ... can you use the "dir" command in your terminal to list the files?

toxic portal
haughty hare
#

yea, that's wrong. it seems whatever you use to create the file with, adds it automatically

toxic portal
#

Can you do this, you should then be able to see the full name, .env.env in file explorer, and can then rename the file to .env

haughty hare
#

yep, always nicer to actually see all your extensions

toxic portal
#

I had a message typed out saying I'm guessing the files named .env.env lol

mortal cove
#

ah! It finally works haha

#

thank you both!

toxic portal
#

Sweet, !close to close I believe

#

all good

gaunt needleBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.

#

๐Ÿ”’ KeyError