加载 Cogs 时出现“ImportError:尝试相对导入超出顶级包”

问题描述 投票:0回答:1

我长期以来一直在寻找具有相同问题的问题,但似乎没有一个能解决我的问题。

我目前正在尝试为我的机器人加载 Cogs,但每当我这样做时,我都会收到错误:“ImportError:尝试相对导入超出顶级包”:

> *Traceback (most recent call last):   File "C:\Users\user\PycharmProjects\Site -
> Projet\venv\lib\site-packages\discord\ext\commands\bot.py", line 935,
> in _load_from_module_spec
>     spec.loader.exec_module(lib)  # type: ignore   File "<frozen importlib._bootstrap_external>", line 850, in exec_module   File
> "<frozen importlib._bootstrap>", line 228, in
> _call_with_frames_removed   File "C:\Users\user\PycharmProjects\Site - Projet\discord-bot\Cogs\base.py", line 2, in <module>
>     from ..main import client ImportError: attempted relative import beyond top-level package The above exception was the direct cause of
> the following exception: Traceback (most recent call last):   File
> "C:\Users\user\PycharmProjects\Site - Projet\discord-bot\main.py",
> line 44, in <module>
>     asyncio.run(main())   File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\asyncio\runners.py",
> line 44, in run
>     return loop.run_until_complete(main)   File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py",
> line 647, in run_until_complete
>     return future.result()   File "C:\Users\user\PycharmProjects\Site - Projet\discord-bot\main.py", line 36, in main
>     await load()   File "C:\Users\user\PycharmProjects\Site - Projet\discord-bot\main.py", line 29, in load
>     await client.load_extension(f'Cogs.{filename[:-3]}')   File "C:\Users\user\PycharmProjects\Site -
> Projet\venv\lib\site-packages\discord\ext\commands\bot.py", line 1013,
> in load_extension
>     await self._load_from_module_spec(spec, name)   File "C:\Users\user\PycharmProjects\Site -
> Projet\venv\lib\site-packages\discord\ext\commands\bot.py", line 938,
> in _load_from_module_spec
>     raise errors.ExtensionFailed(key, e) from e discord.ext.commands.errors.ExtensionFailed: Extension 'Cogs.base'
> raised an error: ImportError: attempted relative import beyond
> top-level package*

我的文件结构如下:

├── Project6 (directory)
     ├── discord-bot (directory)
     |  ├── main.py (file)
     |  ├── Cogs (directory)
     |     └── base.py (file)
     └── Unrelated files

这是在 main.py 中加载齿轮的代码:

async def load():
    for filename in os.listdir('Cogs'):
        if filename.endswith('.py'):
            await client.load_extension(f'Cogs.{filename[:-3]}')
        else:
            print("Couldn't log the Cogs.")

Start = 'TOKEN'

async def main():
    await load()
    await client.start(Start)

if __name__ == "__main__":
    asyncio.run(main())

这是base.py 中的一个

from discord.ext import commands
from ..main import client


class BASE(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

        @client.command()
        async def training(ctx):
            print('a')



def setup(bot):
    client.add_cog(BASE(bot))

我尝试将“from ..main import client”更改为“from main import client”、“from main import *”和“import main”,但这些都不起作用。

python discord.py python-import
1个回答
0
投票

在 base.py 文件中尝试以下操作:

async def setup(bot):
    await client.add_cog(BASE(bot))

并将

client.command
更改为
commands.command

© www.soinside.com 2019 - 2024. All rights reserved.