我写了一个使用 cogs 的 discord 机器人。这是我在每个扩展/齿轮中加载的代码:
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, in <module>
client.load_extension(f'cogs.{filename[:-3]}')
File "C:\Users\indap\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 649, in load_extension
spec = importlib.util.find_spec(name)
File "C:\Users\indap\AppData\Local\Programs\Python\Python38\lib\importlib\util.py", line 94, in find_spec
parent = __import__(parent_name, fromlist=['__path__'])
ModuleNotFoundError: No module named 'cogs'
我检查过,文件路径是正确的,我什至尝试使用不同的文件路径,但我仍然得到同样的错误。
最佳答案
看起来可能是区分大小写的问题。当遍历目录的内容时,你写了 .\Cogs
作为路径,但是在 load_extension()
方法中,你写了 cogs.
。
尝试将其改为 Cogs.
。或者,或者将目录本身全部小写重命名为 cogs
。
关于python - 模块未找到错误 : No module named 'cogs' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62371376/