Skip to content
Advertisement

Key error while running a Python program with getitem

The code below creates this error when ran on Amazon Linux 2. The intent is to have the python program respond to user input from a discord server. I know just enough to get me here about python. Any help is appreciated. They server is running an updated version of Amazon Linux 2.

File "./bot.py", line 65, in <module>
    client.run(os.environ[config.discord_key])
  File "/usr/lib64/python3.7/os.py", line 681, in __getitem__
    raise KeyError(key) from None
KeyError: 'SuperSecretSquirrelStuff-key'

import discord, asyncio, os, boto3, config

client = discord.Client()

ec2 = boto3.resource('ec2')
instance_id = config.instance_id
#Temp
instance = ec2.Instance(instance_id)

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------------')

@client.event
async def on_message(message):
    memberIDs = (member.id for member in message.mentions)
    if client.user.id in memberIDs:
        if 'stop' in message.content:
            if turnOffInstance():
                await client.send_message(message.channel, 'AWS Instance stopping')
            else:
                await client.send_message(message.channel, 'Error stopping AWS Instance')
        elif 'start' in message.content:
            if turnOnInstance():
                await client.send_message(message.channel, 'AWS Instance starting')
            else:
                await client.send_message(message.channel, 'Error starting AWS Instance')
        elif 'state' in message.content:
            await client.send_message(message.channel, 'AWS Instance state is: ' + getInstanceState())
        elif 'reboot' in message.content:
            if rebootInstance():
                await client.send_message(message.channel, 'AWS Instance rebooting')
            else:
                await client.send_message(message.channel, 'Error rebooting AWS Instance')

def turnOffInstance():
    try:
        instance.stop(False, False)
        return True
    except:
        return False

def turnOnInstance():
    try:
        instance.start()
        return True
    except:
        return False

def getInstanceState():
    return instance.state['Name']

def rebootInstance():
    try:
        instance.reboot()
        return True
    except:
        return False


client.run(os.environ[config.discord_key])```

Advertisement

Answer

You are trying to read from a system environment variable, which clearly hasn’t been set hence the KeyError (as SuperSecretSquirrelStuff-key doesn’t exist as a key in os.environ).

I can see that you’ve used the config object before to read, for example, instance_id.

In that case, read directly from your configuration instead of doing os.environ if you want the value of discord_key:

client.run(config.discord_key)


Or alternatively (not sure why you would like to do this) set the environment variable before reading it:

os.environ[config.discord_key] = "VALUE";
client.run(os.environ[config.discord_key])
Advertisement