Python 找不到已安装的模块('slackclient')

Posted

技术标签:

【中文标题】Python 找不到已安装的模块(\'slackclient\')【英文标题】:Python can't find installed module ('slackclient')Python 找不到已安装的模块('slackclient') 【发布时间】:2019-11-28 08:36:06 【问题描述】:

我正在尝试构建一个用于学习目的的 slack 机器人,但是当我运行脚本时出现此错误 ModuleNotFoundError: No module named 'slackclient'

我尝试安装和卸载,然后用python -m pip install slackclient重新安装

import os
import time
import re
from slackclient import SlackClient

# instantiate Slack client
slack_client = SlackClient(os.environ.get('SLACK_BOT_TOKEN'))

if __name__ == "__main__":
    if slack_client.rtm_connect(with_team_state=False):
        print("Starter Bot connected and running!")
        #Read bot's user ID by calling Web API method 'auth.test'
        starterbot_id = slack_client.api_call("auth.test")["user_id"]
        while True:
            command, channel = parse_bot_commands(slackclient.rtm_read())
            if command:
                handle_command(command, channel)
            time.sleep(RTM_READ_DELAY)
    else:
        print("Connection failed. Exception traceback printed above.")


(STARTE~1) C:\Users\enest\Desktop\Slack Bot\starterbot>python starterbot.py
Traceback (most recent call last):
  File "starterbot.py", line 4, in <module>
    from slackclient import SlackClient
ModuleNotFoundError: No module named 'slackclient'

(STARTE~1) C:\Users\enest\Desktop\Slack Bot\starterbot>pip install slackclient
Requirement already satisfied: slackclient in c:\users\enest\desktop\slackb~1\starte~1\lib\site-packages (2.1.0)
Requirement already satisfied: aiohttp>3.5.2 in c:\users\enest\desktop\slackb~1\starte~1\lib\site-packages (from slackclient) (3.5.4)
Requirement already satisfied: chardet<4.0,>=2.0 in c:\users\enest\desktop\slackb~1\starte~1\lib\site-packages (from aiohttp>3.5.2->slackclient) (3.0.4)
Requirement already satisfied: async-timeout<4.0,>=3.0 in c:\users\enest\desktop\slackb~1\starte~1\lib\site-packages (from aiohttp>3.5.2->slackclient) (3.0.1)
Requirement already satisfied: multidict<5.0,>=4.0 in c:\users\enest\desktop\slackb~1\starte~1\lib\site-packages (from aiohttp>3.5.2->slackclient) (4.5.2)
Requirement already satisfied: yarl<2.0,>=1.0 in c:\users\enest\desktop\slackb~1\starte~1\lib\site-packages (from aiohttp>3.5.2->slackclient) (1.3.0)
Requirement already satisfied: attrs>=17.3.0 in c:\users\enest\desktop\slackb~1\starte~1\lib\site-packages (from aiohttp>3.5.2->slackclient) (19.1.0)
Requirement already satisfied: idna>=2.0 in c:\users\enest\desktop\slackb~1\starte~1\lib\site-packages (from yarl<2.0,>=1.0->aiohttp>3.5.2->slackclient) (2.8)

【问题讨论】:

【参考方案1】:

虽然 PyPI 包称为 slackclient,但您使用名称 slack 导入模块:

import slack

或者,例如:

from slack import WebClient

参考:https://github.com/slackapi/python-slackclient/wiki/Migrating-to-2.x

【讨论】:

谢谢,我刚刚意识到我正在遵循的如何构建机器人的指南是使用 1.1.0 包。【参考方案2】:

我遇到了同样的错误,发现问题是我在 Visual Studio Code 中的 python 设置。我需要在 settings.json 文件中将我的 python 路径从 "/usr/bin/python" 更改为 "/usr/local/bin/python3"。更多详情here.

不确定这是否是您使用的编辑器,但基本上每当我按下编辑器右上角的小播放按钮时,在 Visual Studio Code 终端中运行我的 python 文件时,它都会运行命令 @ 987654324@ 而不是文件上的python3

【讨论】:

【参考方案3】:

可以修复:下载C++新版本https://visualstudio.microsoft.com/visual-cpp-build-tools/

【讨论】:

以上是关于Python 找不到已安装的模块('slackclient')的主要内容,如果未能解决你的问题,请参考以下文章

python:已安装python,再安装Anaconda,使用import导入找不到模块

节点找不到已安装的模块

python一些模块的exe安装包在windows的64位系统里识别不到已安装Python目录的解决方法

Rails 6 webpacker找不到已安装模块的模块

尽管已安装模块,但 Node.js 找不到模块

Python升级找不到Tkinter模块