简体   繁体   English

使用 getitem 运行 Python 程序时出现关键错误

[英]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.下面的代码在 Amazon Linux 2 上运行时会产生此错误。目的是让 python 程序响应来自 discord 服务器的用户输入。 I know just enough to get me here about python.我知道的足以让我了解 Python。 Any help is appreciated.任何帮助表示赞赏。 They server is running an updated version of Amazon Linux 2.他们的服务器正在运行 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])```

According your error message, there is no environment variable "SuperSecretSquirrelStuff-key" in your system.根据您的错误信息,您的系统中没有环境变量“SuperSecretSquirrelStuff-key”。

Here is the code can recurrent your problem:这是可以重现您的问题的代码:

import os
print(os.environ["ANY_KEY_NOT_EXIST"])

To resolve this problem, you should add the environment variable "SuperSecretSquirrelStuff-key" into your system.要解决此问题,您应该将环境变量“SuperSecretSquirrelStuff-key”添加到您的系统中。

For example, execute this code line in your terminal shell:例如,在终端 shell 中执行此代码行:

export SuperSecretSquirrelStuff-key=xxx

By the way, the environment key "SuperSecretSquirrelStuff-key" might be invalid.顺便说一下,环境密钥“SuperSecretSquirrelStuff-key”可能无效。

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 ).您正在尝试从系统环境变量中读取,该变量显然尚未设置,因此KeyError (因为SuperSecretSquirrelStuff-key不作为os.environ的键存在)。

I can see that you've used the config object before to read, for example, instance_id .我可以看到您之前使用过config对象来读取,例如instance_id

In that case, read directly from your configuration instead of doing os.environ if you want the value of discord_key :在这种情况下,如果您想要discord_key的值,请直接从您的配置中读取而不是执行os.environ

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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM