#using dotenv
1 messages · Page 1 of 1 (latest)
Depends on what you mean by "safely use"
like, a way that no one can really do any harm with my sensitive info
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
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
Then you can just use dotenv
doesn't seem to work
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
i am looking at the PyPI page, its not very descriptive
thats even more undescriptive, its the same thing I have on the PyPI website 😦
Which part of it are you not understanding? I think it's perfectly described there
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
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```
This is how I usually do it: https://github.com/No767/Kumiko/blob/dev/Bot/kumikobot.py#L13-L15
Bot/kumikobot.py lines 13 to 15
load_dotenv()
DISCORD_BOT_TOKEN = os.getenv("Dev_Bot_Token")```
from dotenv import dotenv_values
config = dotenv_values('.env')
TOKEN = config['TOKEN']
...
You can also do it like this
So I just would have to add the { }
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.