pyslack
pyslack is a Python API client for interacting with the Slack API. Last updated in September 2019 (version 0.5.0), it provides a synchronous interface for basic Slack operations. It has largely been superseded by the official and actively maintained `slack_sdk` library, which offers a broader feature set, asynchronous support, and better alignment with modern Slack API best practices.
Common errors
-
ModuleNotFoundError: No module named 'slack'
cause The `pyslack` library is not installed in the current environment, or the Python interpreter is not pointing to the correct environment.fixEnsure `pyslack` is installed: `pip install pyslack`. Verify your virtual environment is active. -
AttributeError: module 'slack' has no attribute 'Slack'
cause This error can occur if you have the official `slack_sdk` library installed (where the client is `WebClient`) instead of `pyslack`, but are trying to import `Slack` from the `slack` module, which is the class name for `pyslack`.fixIf you intend to use `pyslack`, ensure only `pyslack` is installed. If you intend to use `slack_sdk`, use `from slack_sdk import WebClient`. -
slack.exceptions.SlackAPIError: invalid_auth
cause The provided Slack API token is invalid, revoked, has insufficient scopes for the requested operation, or is a deprecated legacy token that is no longer active.fixVerify your `SLACK_API_TOKEN` is correct and has the necessary permissions. For new apps, generate a Bot User OAuth Token for your Slack app and ensure it has the required scopes. Consider migrating to `slack_sdk` for better token management.
Warnings
- breaking pyslack is no longer actively maintained since its last release (0.5.0) in September 2019. It does not support modern Slack API features, authentication methods (e.g., Socket Mode, granular permissions via Bolt), or asynchronous operations.
- deprecated The library primarily relies on older Slack API token types. Modern Slack applications use Bot User OAuth Tokens and App-Level Tokens, often managed through the Slack Bolt framework, which `pyslack` does not natively support.
- gotcha The package is installed as `pyslack` but the module to import is `slack`. This can lead to `ModuleNotFoundError` if `from pyslack import Slack` is attempted.
Install
-
pip install pyslack
Imports
- Slack
from pyslack import Slack
from slack import Slack
Quickstart
import os
from slack import Slack
SLACK_API_TOKEN = os.environ.get('SLACK_API_TOKEN', 'YOUR_SLACK_API_TOKEN')
# Ensure you replace 'YOUR_SLACK_API_TOKEN' with an actual legacy token or bot token
# and 'CHANNEL_ID' with the ID of the channel you want to post to.
# For modern Slack apps, consider using slack_sdk with Bot User OAuth Tokens.
if not SLACK_API_TOKEN or SLACK_API_TOKEN == 'YOUR_SLACK_API_TOKEN':
print("Error: SLACK_API_TOKEN environment variable not set or is default. Please set your Slack API token.")
else:
try:
client = Slack(token=SLACK_API_TOKEN)
# Example: Post a simple message to a channel
response = client.chat_postMessage(
channel='CHANNEL_ID',
text='Hello from pyslack!'
)
if response.get('ok'):
print(f"Message sent successfully: {response.get('ts')}")
else:
print(f"Error sending message: {response.get('error')}")
# Example: Get information about the authenticated user
user_info = client.auth_test()
if user_info.get('ok'):
print(f"Authenticated as: {user_info.get('user')}")
else:
print(f"Auth test failed: {user_info.get('error')}")
except Exception as e:
print(f"An error occurred: {e}")