Reputation: 13
I'm writing a discord bot and recently decided to move all of the commands into "Cogs". All of the python files within my cog folder import other modules I've written that have helper functions and whatnot in them (I store them in a named cutils). They seem to import fine, but when I try to run the bot, I am getting the following traceback:
Traceback (most recent call last):
File "/home/pi/.local/lib/python3.7/site-packages/discord/ext/commands/bot.py", line 607, in _load_from_module_spec
spec.loader.exec_module(lib)
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/pi/Code/bot/cogs/moderation.py", line 6, in <module>
import cutils.util as util
ModuleNotFoundError: No module named 'cutils'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "./craig.py", line 43, in <module>
client.load_extension(f"cogs.{filename[:-3]}")
File "/home/pi/.local/lib/python3.7/site-packages/discord/ext/commands/bot.py", line 664, in load_extension
self._load_from_module_spec(spec, name)
File "/home/pi/.local/lib/python3.7/site-packages/discord/ext/commands/bot.py", line 610, in _load_from_module_spec
raise errors.ExtensionFailed(key, e) from e
discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.moderation' raised an error: ModuleNotFoundError: No module named 'cutils'
All of my modules in the cutils directory raise this error and I cannot figure out why.
Here is the directory tree:
bot/
cogs/
cutils/ <-- the modules being imported by moderation.py
__init__.py
util.py
md.py
__init__.py
moderation.py <-- the cog extension
bot.py
bot.py
#!/usr/bin/env python3
import discord
from discord.ext import commands
import os
import logging
from cogs.cutils import util
TOKEN = "token"
LOG_MESSAGE_FORMAT = "%(asctime)s:%(levelname)s:%(message)s"
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.load_extension(f"cogs.{extension}")
client.unload_extension(f"cogs.{extension}")
if __name__ == "__main__":
args = util.get_args()
print(f"Running bot.py with args {args}")
logging.basicConfig(
level=args.logging,
format=LOG_MESSAGE_FORMAT
)
for filename in os.listdir("cogs"):
if filename.endswith(".py"):
client.load_extension(f"cogs.{filename[:-3]}")
client.run(TOKEN)
And here is moderation.py
import discord
from discord.ext import commands
import logging
import cutils.util as util
import cutils.md as md
class Moderation(commands.Cog):
def __init__(self, client):
self.client = client
@commands.command()
async def command1(self, ctx):
do_stuff()
def setup(client):
client.add_cog(Moderation(client))
Any ideas?
Upvotes: 1
Views: 1466
Reputation: 73
You would want to still act as if you were importing from bot.py
from cogs.cutils import util
from cogs.cutils import md
Upvotes: 2