问题描述
我编写了一个使用 cogs 的不和谐机器人.这是我在每个扩展程序/cog 中加载的代码:
i wrote a discord bot that uses cogs. here's my code for loading in each extension/cog:
import discord import os from discord.ext import commands 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.unload_extension(f'cogs.{extension}') client.load_extension(f'cogs.{extension}') for filename in os.listdir('.cogs'): if filename.endswith('.py'): client.load_extension(f'cogs.{filename[:-3]}') client.run('token')
我收到以下错误:
traceback (most recent call last): file "c:/users/indap/pycharmprojects/untitled1/venv/include/main.py", line 22, inclient.load_extension(f'cogs.{filename[:-3]}') file "c:usersindapappdatalocalprogramspythonpython38libsite-packagesdiscordextcommandsot.py", line 649, in load_extension spec = importlib.util.find_spec(name) file "c:usersindapappdatalocalprogramspythonpython38libimportlibutil.py", line 94, in find_spec parent = __import__(parent_name, fromlist=['__path__']) modulenotfounderror: no module named 'cogs'
我检查过,文件路径是正确的,我什至尝试使用不同的文件路径,但我仍然得到同样的错误.
i've checked, the file path is correct, and i even tried using different file path but i still get the same error.
推荐答案
看起来可能是区分大小写的问题.在遍历目录的内容时,您已经编写了 .cogs 作为路径,但在 load_extension() 方法中,您已经编写了 cogs..
it looks like it might be a case-sensitive issue. when iterating over the directory's contents, you have written .cogs as the path, but in the load_extension() method, you have written cogs..
尝试将其更改为 cogs..要么,要么将目录本身全部重命名为cogs.
try changing it to cogs. instead. either that, or rename the directory itself all lower-case to cogs.