問題描述
我編寫了一個使用 cogs 的不和諧機(jī)器人.這是我在每個擴(kuò)展程序/cog 中加載的代碼:
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')
我收到以下錯誤:
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:UsersindapAppDataLocalProgramsPythonPython38libsite-packagesdiscordextcommandsot.py", line 649, in load_extension
spec = importlib.util.find_spec(name)
File "C:UsersindapAppDataLocalProgramsPythonPython38libimportlibutil.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.
推薦答案
看起來可能是區(qū)分大小寫的問題.在遍歷目錄的內(nèi)容時,您已經(jīng)編寫了 .Cogs
作為路徑,但在 load_extension()
方法中,您已經(jīng)編寫了 cogs.
.
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.
.
嘗試將其更改為 Cogs.
.要么,要么將目錄本身全部重命名為cogs
.
Try changing it to Cogs.
instead. Either that, or rename the directory itself all lower-case to cogs
.
這篇關(guān)于ModuleNotFoundError:沒有名為“cogs"的模塊的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!