#๐Ÿ”’ .env Failure

50 messages ยท Page 1 of 1 (latest)

uneven gorge
#

(I tried the !close command on my last post and didn't know it locked me out, my bad.)

I'm creating a discorcd bot in VScode and all is going well. Aside the fact it wont allow me to store my bot's key in a .env. I've tried many online way's and it always outputs "None". All needed libraries are downloaded. Screenshot below to see layout.

Test Python Code:

import os
from dotenv import load_dotenv

key = "TEST_VAR"
value = os.environ.get(key)

print(value)

Test .env Code:

TEST_VAR=aaah

Output:

None
lyric pollenBOT
#

@uneven gorge

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.

sudden granite
#

You need to call load_dotenv()

#
import os
from dotenv import load_dotenv

load_dotenv()

key = "TEST_VAR"
value = os.environ.get(key)

print(value)
uneven gorge
sudden granite
#

it loads the .env from the current directory

#

Did you cd to Python first?

uneven gorge
#

I had that originally but I saw online someone not using it so I tried their way and I guess I forgot to add that back.

uneven gorge
#

I'm a bit new to Python

sudden granite
#

Otherwise you need to provide the path to the .env file

#

cd is an os concept

#

easiest fix is to move the .env file up one directory

uneven gorge
sudden granite
#

I mean load_dotenv("./Python/.env")

uneven gorge
sudden granite
#

You saved, right?

uneven gorge
#

Yuh

#

You think it'd be safe to store my key straight up in my .py ๐Ÿ’€

#

It works like that but ik it's probably not safe...

hollow sigil
#

If you put the key in the code, you have to commit it with the code, which causes security problems.

uneven gorge
#

I've tried basically everything

#

Maybe it's my computer

sudden granite
#

Share the new code you have

hollow sigil
#

Unlikely. Make sure you saved the env file, and try using an absolute path to rule out relative path issues.

sudden granite
#

looks like dotenv.find_dotenv (used by load_dotenv) will actually start looking starting at the caller.

uneven gorge
#

hmmm

uneven gorge
sudden granite
#

You can also check the value of os.getcwd() by printing it

#

If you launched with vscode, it should be the project folder

uneven gorge
#

alright.

sudden granite
#

Can you post a screenshot of your entire vscode window?

uneven gorge
sudden granite
#

Those white circles in the tabs mean you haven't saved.

uneven gorge
#

mmMMmmm, I'm BRAND new to VSc (WEnt from android studio) so I didn't realize that saving saved only 1 tab.

#

Oh well it's somewhat working now.

hollow sigil
#

We almost need to put "make sure you saved the file" as a pre-ask instruction. I've seen like 5+ issues on here and Reddit over the past couple of days that boil down to unsaved files.

uneven gorge
#

Only issue is it isn't printing "aaah"

#

It's printing quite literally "TEST_VAR"

#
import os
from dotenv import load_dotenv

load_dotenv()

key = "TEST_VAR"
value = os.getenv("TEST_VAR")


print(key)
hollow sigil
#

print(key)

uneven gorge
#

oh lord

#

๐Ÿ˜ญ

#

W thanks for the help, wasn't a PC or code problem, just new to VSC problem ๐Ÿคฆโ€โ™‚๏ธ

#

!close

lyric pollenBOT
#
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.