NameError when trying to access environment variable for Discord bot token

I’m encountering a NameError while attempting to launch my Discord bot. The error indicates that DISCORD_TOKEN is not defined, despite it being present in my environment file.

Here’s my primary bot script:

import os
import discord
from dotenv import load_dotenv

load_dotenv()
token = os.getenv(BOT_TOKEN)

client = discord.Client()

@client.event
async def on_ready():
    print(f'{client.user} is now connected!')

client.run(token)

My .env file includes:

BOT_TOKEN = "my_actual_token_here"

I’m continuously receiving this traceback error:

Traceback (most recent call last):
  File "discord_bot.py", line 6, in <module>
    token = os.getenv(BOT_TOKEN)
NameError: name 'BOT_TOKEN' is not defined

All files are located in the same directory. What could be the issue? Appreciate any assistance!

You need quotes around the environment variable name. Python thinks BOT_TOKEN is an undefined variable instead of a string. Use os.getenv('BOT_TOKEN') with quotes.

I’ve hit this exact error when I started with Discord bots a couple years back. Watch out for spacing in your .env file too. Sometimes BOT_TOKEN = "value" breaks depending on how dotenv parses it. I stick with BOT_TOKEN=actual_token_without_quotes to avoid parsing headaches.

Also throw in some error handling - check if the token is None before calling client.run(). Saves debugging time when env files don’t load right in different deployments.

You’re passing the variable name without quotes. Python thinks BOT_TOKEN is a variable, not a string.

Change this:

token = os.getenv(BOT_TOKEN)

To this:

token = os.getenv('BOT_TOKEN')

The environment variable name needs quotes.

Discord bots get messy fast when you scale up. I run multiple bots and constantly deal with token rotation, webhook management, and connecting Discord to other services.

Latenode changed everything for me. You can build Discord bot logic visually, manage all your environment variables in one spot, and connect Discord to databases or APIs without Python deployment headaches.

The visual workflow builder makes handling Discord events and triggering cross-platform actions dead simple. No more environment file nightmares.

You’re missing quotes around the variable name in your os.getenv() call. Python thinks BOT_TOKEN is an undefined variable instead of a string.

Should be:

token = os.getenv('BOT_TOKEN')

I hit this exact issue when switching from hardcoded tokens to environment variables. Heads up - some hosting platforms handle .env files weird compared to local dev. On my VPS, I just set the environment variables directly through the system instead of using .env files for production.

Add a fallback check after loading the token so your bot doesn’t crash silently if the environment variable doesn’t load.

classic mistake - os.getenv(BOT_TOKEN) should be os.getenv('BOT_TOKEN') with quotes. python’s looking for a variable called BOT_TOKEN instead of the string. been there! also, remove the quotes from your .env file - just do BOT_TOKEN=your_token_here without quotes around the token value.