Search code examples
pythondiscorddiscord.py

Extension 'cogs.__pycach' could not be loaded. discord.py


I'm trying to set up my python bot to work with my extensions that are in the folder "cogs". However, the following error happens while launching it

discord.ext.commands.errors.ExtensionNotFound: Extension 'cogs.__pycach' could not be loaded.

I'm using the following code to load and unload my extensions:

async def load(ctx, extension):
    bot.load_extension(f'commands.{extension}')

@bot.command()
async def unload(ctx, extension):
    bot.unload_extension(f'cogs.{extension}')
for filename in os.listdir('./cogs'):
    bot.load_extension(f'cogs.{filename[:-3]}')

This problem is however fixed if I delete the pycache folder, but the folder has to be deleted every time the bot restarts.


Solution

  • You're code is trying to load everything that's in this directory, so in this example the code try to load _pycache_ which I think is not a cog :P. You could try this one:

    extensions = [
        '[modulename]',
        ]
    

    e.g.

    extensions = [
        'data.testCog'
        ]
    

    This "." (dot) is used to mark the directory (if testCog.py file is in data folder). Then you can make an if and for loop:

    if __name__ == '__main__':
        for extension in extensions:
            bot.load_extension(extension)