【问题标题】:Python can't find "__main__" modulePython 找不到“__main__”模块
【发布时间】:2020-09-07 15:19:00
【问题描述】:

当我尝试运行我的代码时,出现此错误:\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\python.exe: can't find '\_\_main\_\_' module in ''

我不知道如何解决它。我看过与我类似的帖子,但他们的修复似乎不起作用。这是我所有的代码:

import discord

client = discord.client()

@client.event  
async def on_ready():
    print(f"we have logged in as {client.user}")

client.run("my bot token would be here")

@client.event
async def on_message(message):  # event that happens per any message.

    # each message has a bunch of attributes. Here are a few.
    # check out more by print(dir(message)) for example.
    print(f"{message.channel}: {message.author}: {message.author.name}: {message.content}")


client.run(token)  # recall my token was saved!

@client.event
async def on_message(message):  # event that happens per any message.
    print(f"{message.channel}: {message.author}: {message.author.name}: {message.content}")
    if str(message.author) == "hello" in message.content.lower():
        await message.channel.send('hi')
        

【问题讨论】:

  • 你安装了discord lib吗?和蟒蛇?您能否提供有关您的环境和执行的更多信息?此外,我强烈建议您更改为 Unix 环境。
  • discord lib 和 python 都已安装。我正在使用崇高的文本,所以我只需编写代码并运行,剩下的就交给我了。我对编码很陌生
  • 请发布完整的回溯。我们需要详细信息。
  • 我不太清楚你的意思,我所有的代码都在那里。如果你是这个意思,这里是完整的错误。 C:\Users\zorbs\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\python.exe:在“”中找不到“main”模块
  • @NoHaxJustEncrypted - 很有趣。这表明该脚本从未运行过,问题出在您的安装上。

标签: python discord


【解决方案1】:

非常简单的修复。您已包含两个 client.events。 discord.py 不允许这样做,人们最终会得到最随机的错误。我也有这个问题有一段时间了。您需要指定不和谐机器人正在寻找什么。这是调整后的代码,您可以根据需要进行更改。 在您的代码末尾,您需要添加 await client.process_commands(message),因为这将确保机器人考虑所有可能性,并且您的命令以及事件都可以正常工作。

import discord

client = discord.client()

@client.event  
async def on_ready():
    print(f"we have logged in as {client.user}")

client.run("my bot token would be here")

@client.event
async def on_message(message):  # event that happens per any message.

    # each message has a bunch of attributes. Here are a few.
    # check out more by print(dir(message)) for example.

    if " " in message.content: #Basically it'll work if the message contains " "
        print(f"{message.channel}: {message.author}: {message.author.name}: {message.content}")

    if str(message.author) == "hello" in message.content.lower(): #This is good because you are specifying what the bot should be looking for ("hello")
        await message.channel.send('hi')
    await client.process_commands(message)


client.run(token)  # recall my token was saved!

另一种可能性:client.run 应该位于代码的末尾且仅位于末尾。从 on_ready 函数中删除它,那里不需要它。

【讨论】:

    猜你喜欢
    • 2016-10-31
    • 1970-01-01
    • 2021-01-26
    • 2020-05-10
    • 1970-01-01
    • 1970-01-01
    • 2019-08-27
    • 1970-01-01
    相关资源
    最近更新 更多