#using dotenv

1 messages · Page 1 of 1 (latest)

thick sierra
#

is there a way I can safely use dotenv in my code? I looked on google, but they are all different from one another, and the ones I tried are not working.

hardy zenith
#

Depends on what you mean by "safely use"

thick sierra
#

like, a way that no one can really do any harm with my sensitive info

hardy zenith
#

I mean the only reason to use dotenv is to store you sensitive values in a separate file so that you can share your other files publicly.

#

If you're not sharing your files anywhere it's not gonna make a difference if you use dotenv or just use the values in your code

thick sierra
#

I plan on sharing my main code with someone, but don't want to share my .env so I want it to not have the values in the code itsself

hardy zenith
#

Then you can just use dotenv

thick sierra
hardy zenith
#

you're installing dotenv, but you're using os to get an environment variable

#

I'd recommend you to look at some examples of how to use dotenv

thick sierra
thick sierra
#

thats even more undescriptive, its the same thing I have on the PyPI website 😦

hardy zenith
#

Which part of it are you not understanding? I think it's perfectly described there

grim warren
#

Hi Daniel, I use an .env file in my project, too.
You're going to want to use os.getenv("{your env variable}")

If you want to see an example, you can check how I do it here
https://github.com/Kilvoctu/aiyabot/blob/be88d1ea7dde38671679861c1551b556a1c2c742/core/settings.py#L62-L72
This is a separate file from my main file where I do load_dotenv(), then I run this startup_check() for the .env variables
The function looks at the .env for the things I want, then stores into global variables.

So you can use something like this, then share your application and tell them how to set up their .env file

tired fableBOT
#

core/settings.py lines 62 to 72

def startup_check():
    #check .env for parameters. if they don't exist, ignore it and go with defaults.
    global_var.url = get_env_var_with_default('URL', 'http://127.0.0.1:7860').rstrip("/")
    print(f'Using URL: {global_var.url}')

    global_var.dir = get_env_var_with_default('DIR', 'outputs')
    print(f'Using outputs directory: {global_var.dir}')

    global_var.username = os.getenv("USER")
    global_var.password = os.getenv("PASS")
    global_var.copy_command = os.getenv("COPY") is not None```
vestal pasture
tired fableBOT
#

Bot/kumikobot.py lines 13 to 15

load_dotenv()

DISCORD_BOT_TOKEN = os.getenv("Dev_Bot_Token")```
hardy zenith
#
from dotenv import dotenv_values

config = dotenv_values('.env')

TOKEN = config['TOKEN']
...

You can also do it like this

thick sierra
hardy zenith
#

No

#

The brackets should not be there, it's just to show that it's a placeholder

grim warren
#

Yeah, it was me showing it's a placeholder. It's os.getenv("") then the text inside the quotations corresponds to what you have in the .env file.

noelle have the simple example with the bot token, which I suppose is what most people would use it for.
So using that example, her .env file would have Dev_Bot_Token= and end user would put their bot token there.
When the code runs, os.getenv("Dev_Bot_Token") will look at the .env file to see what value is assigned to Dev_Bot_Token=
Then it'll store that value into DISCORD_BOT_TOKEN variable to use as needed.