Discord.py Cog扩展引发了ModuleNotFoundError吗?

时间:2020-08-29 00:11:24

标签: python python-3.x discord.py

我正在写一个不和谐的机器人,最近决定将所有命令移到“齿轮”中。我在cog文件夹中的所有python文件都会导入我编写的其他具有辅助功能的模块,这些模块中没有其他功能(我将它们存储在名为 cutils 的位置)。它们似乎可以正常导入,但是当我尝试运行bot时,我得到了以下追溯:

Traceback (most recent call last):
  File "/home/pi/.local/lib/python3.7/site-packages/discord/ext/commands/bot.py", line 607, in _load_from_module_spec
    spec.loader.exec_module(lib)
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/pi/Code/bot/cogs/moderation.py", line 6, in <module>
    import cutils.util as util
ModuleNotFoundError: No module named 'cutils'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "./craig.py", line 43, in <module>
    client.load_extension(f"cogs.{filename[:-3]}")
  File "/home/pi/.local/lib/python3.7/site-packages/discord/ext/commands/bot.py", line 664, in load_extension
    self._load_from_module_spec(spec, name)
  File "/home/pi/.local/lib/python3.7/site-packages/discord/ext/commands/bot.py", line 610, in _load_from_module_spec
    raise errors.ExtensionFailed(key, e) from e
discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.moderation' raised an error: ModuleNotFoundError: No module named 'cutils'

cutils目录中的所有模块都引发此错误,我无法弄清原因。

这是目录树:

bot/
  cogs/
    cutils/ <-- the modules being imported by moderation.py
      __init__.py
      util.py
      md.py
    __init__.py
    moderation.py <-- the cog extension
  bot.py

bot.py

#!/usr/bin/env python3

import discord
from discord.ext import commands

import os
import logging

from cogs.cutils import util

TOKEN = "token"
LOG_MESSAGE_FORMAT = "%(asctime)s:%(levelname)s:%(message)s"

client = commands.Bot(command_prefix="!")

@client.command()
async def load(ctx, extension):
    client.load_extension(f"cogs.{extension}")

@client.command()
async def unload(ctx, extension):
    client.unload_extension(f"cogs.{extension}")

@client.command()
async def reload(ctx, extension):
    client.load_extension(f"cogs.{extension}")
    client.unload_extension(f"cogs.{extension}")

if __name__ == "__main__":

    args = util.get_args()
    print(f"Running bot.py with args {args}")

    logging.basicConfig(
        level=args.logging,
        format=LOG_MESSAGE_FORMAT
    )
    
    for filename in os.listdir("cogs"):
        if filename.endswith(".py"):
            client.load_extension(f"cogs.{filename[:-3]}")
    
    client.run(TOKEN)

这是moderation.py

import discord
from discord.ext import commands

import logging

import cutils.util as util
import cutils.md as md

class Moderation(commands.Cog):

    def __init__(self, client):
        self.client = client

    @commands.command()
    async def command1(self, ctx):
        do_stuff()
    
def setup(client):
    client.add_cog(Moderation(client))

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您仍然希望像从 bot.py

导入一样
from cogs.cutils import util
from cogs.cutils import md