【问题标题】:ImportError: cannot import name 'Bot' from 'telegram'ImportError:无法从“电报”导入名称“Bot”
【发布时间】:2021-06-24 00:27:04
【问题描述】:

我正在尝试向 Heroku 部署一个简单的呼叫和响应机器人,但我不断收到相同的错误,我不知道如何解决它。我发现该程序可以在我的个人计算机上运行,​​但当我将它部署到 Heroku 时却不行。我觉得这与我的导入语句有关:

import random
from telegram.ext import Updater
from telegram.ext import CommandHandler
from telegram.ext import MessageHandler
from telegram.ext import Filters
from telegram.ext import messagequeue as mq
from telegram.utils.request import Request
import logging
import os

在推送到 heroku 并运行它后,我得到了这些错误:

2021-03-27T08:25:40.562359+00:00 heroku[web.1]: Starting process with command `python3 bog_bot.py`
2021-03-27T08:25:43.167956+00:00 heroku[web.1]: Process exited with status 1
2021-03-27T08:25:43.257029+00:00 heroku[web.1]: State changed from starting to crashed
2021-03-27T08:25:43.102105+00:00 app[web.1]: Traceback (most recent call last):
2021-03-27T08:25:43.102177+00:00 app[web.1]: File "/app/bog_bot.py", line 2, in <module>
2021-03-27T08:25:43.102489+00:00 app[web.1]: from telegram.ext import Updater
2021-03-27T08:25:43.102543+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/telegram/ext/__init__.py", line 21, in <module>
2021-03-27T08:25:43.102788+00:00 app[web.1]: from .basepersistence import BasePersistence
2021-03-27T08:25:43.102820+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/telegram/ext/basepersistence.py", line 25, in <module>
2021-03-27T08:25:43.103058+00:00 app[web.1]: from telegram import Bot
2021-03-27T08:25:43.103163+00:00 app[web.1]: ImportError: cannot import name 'Bot' from 'telegram' (/app/.heroku/python/lib/python3.9/site-packages/telegram/__init__.py)

我非常感谢任何帮助,因为我已经为这个小问题寻找了几个小时的答案。

【问题讨论】:

  • 我不是专家,但你有没有文件requirements.txt?devcenter.heroku.com/articles/python-pip 你需要列出你想在其中使用的python标准库中没有的模块。跨度>
  • 是的,我可以在这里包含它:telegram==0.0.1 appdirs==1.4.4 APScheduler==3.6.3 certifi==2020.12.5 chardet==4.0.0 distlib==0.3 .1 filelock==3.0.12 idna==2.10 importlib-metadata==1.5.0 more-itertools==4.2.0 pipenv==11.9.0 pyTelegramBotAPI==3.7.6 python-telegram-bot==13.4.1 pytz==2021.1 requests==2.25.1 六==1.14.0 telebot==0.0.4 tornado==6.1 tzlocal==2.1 urllib3==1.26.4 virtualenv==20.4.3 virtualenv-clone==0.3.0 zipp==1.0.0

标签: python python-3.x heroku telegram telegram-bot


【解决方案1】:

尝试删除

telegram==0.0.1 

更新

python-telegram-bot 模块使用命名空间telegram。因此可能会导致同时安装两个模块时出错。要解决这个问题,您需要卸载 telegram 模块。由于从requirements.txt 中删除模块不会自动删除该模块,因此您需要自行删除该模块。见:

Manually remove Python package on Heroku

参考: https://github.com/python-telegram-bot/python-telegram-bot/issues/395

【讨论】:

  • @JonathanBear 从我读到的与这个问题相关的帖子中,解决方案都是关于 python-telegram-bot 没有安装(或安装正确)。还是模块版本不匹配?
  • 我的需求文件直接来自我的 pipenv freeze,如果这里没有正确安装,我不确定如何正确安装文件
  • 你能做到heroku run pip list吗?另见github.com/fabston/TradingView-Webhook-Bot/issues/5
  • 它在那里,版本 13.4.1
  • 所以,我不确定您是否从 requirements.txt 中删除了一个模块,是否会从 heroku 中卸载该模块。 heroku 中还有 telegram==0.0.1 吗?
【解决方案2】:

所以我发现问题不是需求文件中的错误,而是 pipfile 中的错误。我从那里删除了一堆要求,并且效果更好。不过,我仍然包含了电报库,我认为可能是 pyTelegramBotAPI 库导致了我的问题。我还在另一个应用程序上重新启动了该项目,并对机器人的代码进行了一些更改。现在导入部分如下所示:

import random
from telegram import Update
from telegram.ext import Updater
from telegram.ext import CommandHandler
from telegram.ext import MessageHandler
from telegram.ext import Filters
from telegram.ext import messagequeue as mq
import telegram
from telegram.utils.request import Request
import logging
import os

【讨论】:

    【解决方案3】:

    确保您的 Pipfile 和 requirements.txt 都只有 python-telegram-bot 库。

    如果还有telerampython-telegram等其他相关库,就会出现这种错误。

    例如:ImportError: cannot import name 'Animation' from 'telegram' (/app/.heroku/python/lib/python3.9/site-packages/telegram/init.py)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-22
      • 2021-06-30
      • 2020-10-19
      • 2018-08-13
      • 2015-01-26
      • 2020-10-10
      • 2020-02-12
      • 2021-11-27
      相关资源
      最近更新 更多