Skip to main content
 首页 » 编程设计

python之属性错误 : 'module' object has no attribute 'packages' Pycharm 5. 0.1

2024年10月24日9bjzhanghao

我正在尝试制作一个简单的电报机器人,它只会发回一些文本。我正在使用 Pycharm。 我的程序:

import time 
import telepot 
 
def add(chat_id, msg): 
bot.sendMessage(chat_id, 'add') 
 
def age(chat_id, msg): 
    bot.sendMessage(chat_id, 'age') 
 
def remove(chat_id, msg): 
    bot.sendMessage(chat_id, 'remove') 
 
def birthday(chat_id, msg): 
    bot.sendMessage(chat_id, 'birthday') 
 
def handle(msg): 
    chat_id = msg['chat']['id'] 
    command = msg['text'] 
 
    print('Got command: %s' % command) 
    print(chat_id) 
 
    if '/birthday' in command: 
        birthday(chat_id, msg) 
    elif '/add' in command: 
        add(chat_id, msg) 
    elif '/remove' in command: 
        remove(chat_id, msg) 
    elif '/age' in command: 
        age(chat_id, msg) 
 
bot = telepot.Bot('<My token>') 
bot.notifyOnMessage(handle) 
print('I am listening ...') 
 
while 1: 
    time.sleep(10) 

如果我在终端中运行我的程序,它工作正常,但如果我尝试使用 Pycharm 运行它,这就是我得到的结果。

/usr/bin/python3.4 /home/omer/PycharmProjects/RelAgeBot/RelAgeBot.py 
Traceback (most recent call last): 
  File "/home/omer/PycharmProjects/RelAgeBot/RelAgeBot.py", line 17, in <module> 
    import telepot 
  File "/usr/local/lib/python3.4/dist-packages/telepot/__init__.py", line 17, in <module> 
    requests.packages.urllib3.disable_warnings() 
AttributeError: 'module' object has no attribute 'packages' 
 
Process finished with exit code 1 

我已经在终端上安装了带有 pip 的“telepot”包,并再次使用 Pycharm,但它似乎不起作用。

请您参考如下方法:

为了回答我自己的问题,在@dursk 评论说问题可能出在模块“requests”之后,我尝试再次更新它。我不知道确切原因,但 Pycharm 没有更新此模块。在我尝试更新它几次之后,它被更新了。现在可以了。

因此,在 Pycharm 中转到“设置”>“项目”>“项目解释器”。在此列表中搜索您要查找的包并双击它。单击选项以下载特定版本并选择最新版本。然后点击安装就完成了