问题
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')
And I'm getting the following error:
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'
I've checked, the file path is correct, and I even tried using different file path but I still get the same error.
回答1:
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.
.
Try changing it to Cogs.
instead. Either that, or rename the directory itself all lower-case to cogs
.
回答2:
Discord module should have automatically installed cogs but it seems like it has not. Try installing it manually
pip install Cogs
来源:https://stackoverflow.com/questions/62371376/modulenotfounderror-no-module-named-cogs