So I am trying to get my bot token from token.env but I get a compiler error. client.run(os.getenv("TOKEN"))
and then my token.env is this TOKEN["insert token here"]
I'm not 100% sure how to format that. Thanks in advance.
.env files are a deprecated method from replit, If you go to your sidebar and click on the lock icon (middle of the list), you'll see a tab called secrets, then all you need to do is make the key whatever, and put your token as the value, click "add new secret", then use
import os
token = os.environ['DISCORD_TOKEN']
Replace DISCORD_TOKEN with whatever you decide to name it, then to run it's just
client.run(token)
which is much more readable
Make a.env file in the current directory, your file content should look like this
TOKEN_KEY=TOKEN_VALUE
Install python-dotenv
module
pip install python-dotenv
Add these two line in you Discord.py file
from dotenv import load_dotenv
load_dotenv()
This is work fine.
You can use a libary/module called python-dotenv
, install the library with
pip install python-dotenv
To use it in your code, you have to import the os module as well as the freshly installed dotenv package
import os
from dotenv import load_dotenv
At the beginning of your code after the imports you should have load_dotenv()
to load the.env file. Then you can use os.getenv("DOTENV variablename here")
to get the content of the file.
Instruction List:
pip install python-dotenv.
Create a file named.env in the root of your project. Write one line: DISCORD_TOKEN = your token
(no quotes needed) you should have import os and from dotenv import load_dotenv
in your code. Call load_dotenv()
at the beginning of your program to load the file. To get your token, you just have to do
os.getenv("DISCORD_TOKEN").
Example code:
import os
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")
Example dotenv file:
DISCORD_TOKEN=this.is.my.token.blah.blah.blah
Answer taken from here: How would I go about creating an.env file for my discord bot token?
Try making the file name .env
instead of token.env
Edit: If that doesn't work, you could try this to make a new.env;
How would I go about creating an.env file for my discord bot token?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.