【问题标题】:Getting python file outside of folder获取文件夹外的python文件
【发布时间】:2021-06-19 16:31:02
【问题描述】:

我正在尝试导入我的主要 python 文件,但它不起作用。

import discord
from discord.ext import commands
from ..bot import bot
class help(commands.Cog):
    
    def __init__(self, client):
        self.client = client
    @commands.command(name="help", aliases=["Help"])
    async def help(self,context, message = None):
        embed = discord.Embed(title="Help command")
        cmds = ""
        if message == None:
            bot.listcom(cmds)
            embed.add_field(name="Commands", value=cmds)
        if not message == None:
            pass
        await context.send(embed=embed)

def setup(client):
    client.add_cog(help(client))

终端输出:

ImportError: attempted relative import with no known parent package

我尝试使用 import ..bot 但它给出了语法错误,当我尝试仅使用 import bot 时它说它不存在。有人可以帮帮我吗?

【问题讨论】:

  • 相对导入仅适用于包内的子模块。如果您显示您的文件夹结构可能会有所帮助。

标签: python-3.x discord.py


【解决方案1】:

https://stackoverflow.com/a/24868877/16237426
发现..

在你的情况下应该可以工作:

import sys, os
sys.path.append(os.path.abspath(os.path.join('..')))
from bot import bot

【讨论】:

    猜你喜欢
    • 2016-01-27
    • 2020-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-03
    • 2014-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多