【问题标题】:What's wrong with my code? I can run this fine on my vscode but it won't work on heroku, the prob comes from this code我的代码有什么问题?我可以在我的 vscode 上运行这个很好,但它不能在 heroku 上运行,问题来自这个代码
【发布时间】:2020-12-10 07:59:35
【问题描述】:

我的代码有什么问题?我可以在我的 vscode 上运行这个很好,但它不会在 heroku 上运行,问题来自这个代码

COGS = [path.split("\\")[-1][:-3] for path in glob("./lib/cogs/*.py")]

class Bot(BotBase):
    def __init__(self):
        self.PREFIX = PREFIX
        self.ready= False
        self.cogs_ready = Ready()

        self.guild = None
        self.scheduler = AsyncIOScheduler()

        db.autosave(self.scheduler)
        super().__init__(command_prefix=PREFIX, owner_ids=OWNER_IDS)

    def setup(self):
        for cog in COGS:
            self.load_extension(f"lib.cogs.{cog}")
            print(f"{cog} cog loaded")
        print("setup complete")

我得到的错误是

2020-08-20T22:51:05.100470+00:00 app[Worker.1]:     self.setup()
2020-08-20T22:51:05.100473+00:00 app[Worker.1]:   File "/app/lib/bot/__init__.py", line 56, in setup
2020-08-20T22:51:05.100618+00:00 app[Worker.1]:     self.load_extension(f"lib.cogs.{cog}")
2020-08-20T22:51:05.100621+00:00 app[Worker.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 660, in load_extension
2020-08-20T22:51:05.100910+00:00 app[Worker.1]:     spec = importlib.util.find_spec(name)
2020-08-20T22:51:05.100913+00:00 app[Worker.1]:   File "/app/.heroku/python/lib/python3.8/importlib/util.py", line 94, in find_spec
2020-08-20T22:51:05.101051+00:00 app[Worker.1]:     parent = __import__(parent_name, fromlist=['__path__'])
2020-08-20T22:51:05.101070+00:00 app[Worker.1]: ModuleNotFoundError: No module named 'lib.cogs.'
2020-08-20T22:51:05.196203+00:00 heroku[Worker.1]: Process exited with status 1
2020-08-20T22:51:05.234805+00:00 heroku[Worker.1]: State changed from up to crashed

请帮忙

【问题讨论】:

    标签: python heroku visual-studio-code discord discord.py


    【解决方案1】:

    您错误地拆分了路径。目前您的代码仅适用于 Windows。

    您需要使用os.path.sep 而不是硬编码"\\",否则在目录分隔符为"/" 的Linux 上会失败。

    但是使用os.path.split() 可能会更好,而不是手动弄乱路径。总的来说看看the os.path functions,你可能对他们有用!

    【讨论】:

      猜你喜欢
      • 2021-04-21
      • 1970-01-01
      • 1970-01-01
      • 2021-04-04
      • 1970-01-01
      • 2018-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多