I'm trying to make a discord bot, and when I try to load a .env with load_dotenv() it doesn't work because it says
Traceback (most recent call last):
File "/home/fanjin/Documents/Python Projects/Discord Bot/bot.py", line 15, in <module>
client.run(TOKEN)
File "/home/fanjin/.local/lib/python3.8/site-packages/discord/client.py", line 708, in run
return future.result()
File "/home/fanjin/.local/lib/python3.8/site-packages/discord/client.py", line 687, in runner
await self.start(*args, **kwargs)
File "/home/fanjin/.local/lib/python3.8/site-packages/discord/client.py", line 650, in start
await self.login(*args, bot=bot)
File "/home/fanjin/.local/lib/python3.8/site-packages/discord/client.py", line 499, in login
await self.http.static_login(token.strip(), bot=bot)
AttributeError: 'NoneType' object has no attribute 'strip
Here's my code for the bot:
import os
import discord
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
client = discord.Client()
@client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
client.run(TOKEN)
And the save.env file: (It's a fake token)
# .env
DISCORD_TOKEN={XXXXXXXX}
Both files are in the same directory, and I even tried to explicitly specify the .env's path with
env_path = Path('path/to/file') / '.env'
load_dotenv(dotenv_path=env_path)
but that also didn't work
dotenv
library, which isn't part of Python or Linux itself, consider tagging for that library. – PhilcoxDISCORD_TOKEN='{...}'
– PhilcoxNone
instead of containing a string. – Philcoxpython-dotenv
library I installed with pip. I tried quoting it but it didn't work, and I updated the post to include the full stack trace – Filidetoken
is None; it's not being directly thrown from the dotenv code itself. – PhilcoxEnvironmentFile
directive to have systemd load the variables from your file before the Python interpreter is even started? – PhilcoxEnvironmentFile
directive? – Filideos.getcwd()
) is the same as the directory that contains both your code and your.env
. That's not necessarily the case, and there are good reasons to make it not the case (a cwd of/
avoids whole classes of bugs, f/e, the potential for a working directory to become invalid). – Philcox.service
file that tells you how you want that service to be run (so if you called itmy-discord-bot.service
, you could then usesystemctl start my-discord-bot
to start it, orjournalctl -u my-discard-bot
to read its logs). – Philcox