Ask coding questions

← Back to all posts
Anyone know how to use .env?(
tristandasavage (1)

I'm trying to hide my token in a .env file so people can't get my bot's token, and I don't know how to get my token variable from the file.
TOKEN=da token(not telling u lol)
token = os.getenv("TOKEN")

I have a feeling that my code is wrong, but I don't know what the right code is.

Answered by AloegelhiPlaysR (131) [earned 5 cycles]
View Answer
AloegelhiPlaysR (131)

UH.. Delete secrets.env NOW. Your token is public right now! Make a new file called .env and do token=YOUR TOKEN

We have full access to it.

and yes, os.getenv("token") is correct.

KHZ (122)

@TristanMurphy You don't need to name the .env file. While adding a new file to the repl you just write as .env as the filename not vars.env.

AloegelhiPlaysR (131)

Also please go here and regenerate your bot token! @AloegelhiPlaysR

EpicRaisin (264)

Yes, I know that this question has already been answered, but no one has explicitly explained why your file didn't work, so I'm gonna answer that.

So .env files, of course, can only be seen by the repl owner. When making a .env file, only name it .env. It's not supposed to be an extension name.

When you call for a variable from .env with os.getenv or os.environ.get, it'll look file a file called .env. So make sure the file is called that. will automatically hide that file if it has the correct name.

luigirandoli (0)

@EpicRaisin idk if you are still getting replies, but when i try and create a file thats only called this, it does not let me.

EpicRaisin (264)

@luigirandoli changed the system so now you don't need to make a file. Instead, you just go to the Secrets tab (the one with the lock) to make secrets, and it'll still work with the same code.

AloegelhiPlaysR (131)

@TristanMurphy Here, join the team I invited you to. I'll help ya out for a sec. You'll be surprised.

tristandasavage (1)

@AloegelhiPlaysR btw is there a chance that you know how I can run it 24/7 using uptimerobot? I already have it configured in the dashboard, but I paused it so I could fix this. I noticed that when I run it, I get an error after it says updating
Traceback (most recent call last):
File "", line 169, in <module>
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/", line 708, in run
return future.result()
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/", line 687, in runner
await self.start(args, **kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/", line 650, in start
await self.login(
args, bot=bot)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/", line 499, in login
await self.http.static_login(token.strip(), bot=bot)
AttributeError: 'NoneType' object has no attribute 'strip'

tristandasavage (1)

lol nvm im an idiot and put DISCORD_BOT_TOKEN instead of DISCORD_BOT_SECRET which was in the variable lol

tristandasavage (1)

@InvisibleOne lol I saw that and did it accordingly, but it didn't work when I did it that way

Coder100 (18813)

It is already hidden. Only you can see it.