Abhishek Indapure
Abhishek Indapure

Reputation: 81

ModuleNotFoundError: No module named 'cogs'

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.

Upvotes: 3

Views: 6715

Answers (1)

Diggy.
Diggy.

Reputation: 6944

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.

Upvotes: 2

Related Questions