python - 模块未找到错误 : No module named 'cogs'

标签 python python-3.x discord discord.py

我写了一个使用 cogs 的 discord 机器人。这是我在每个扩展/齿轮中加载的代码:

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:\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'

我检查过,文件路径是正确的,我什至尝试使用不同的文件路径,但我仍然得到同样的错误。

最佳答案

看起来可能是区分大小写的问题。当遍历目录的内容时,你写了 .\Cogs 作为路径,但是在 load_extension() 方法中,你写了 cogs.

尝试将其改为 Cogs.。或者,或者将目录本身全部小写重命名为 cogs

关于python - 模块未找到错误 : No module named 'cogs' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62371376/

相关文章:

Python-numpy : Specifying item size of a compound data type doesn't work

python字典可变澄清

python - 使用 python 求解嵌套函数(循环方程)?

python - 从 python 字典中的列表中检索值时遇到问题

python - NLTK 中的斯坦福 NER 未正确标记多个句子 - Python

python - 在 shebang python26 或 python2.6 中引用什么

python - 为什么 py.test 向后运行模块 doctests?

python - 当数组数据类型和格式说明符不匹配时,如何将数组堆栈保存到 .csv?

discord - discord.py 和 discord.py 的区别[语音]

javascript - 机器人不断重复