Troubleshooting Python SDK

This page explains how to identify your SDK version and handle errors in the PubNub Python Software Development Kit (SDK).

How to find the version of your SDK

Access your SDK version via constant

The following snippet demonstrates how to retrieve and display the SDK version using the PubNub class.

Steps to access the SDK version

  1. Import the required modules.
  2. Retrieve the version with PubNub.SDK_VERSION.
  3. Log or print the version for future reference.
import os
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub

def main():
# Display the current SDK version
print(f"Current PubNub SDK Version: {PubNub.SDK_VERSION}")

# Optional: You can also initialize a PubNub instance and check the version
pn_config = PNConfiguration()
pn_config.subscribe_key = os.getenv('SUBSCRIBE_KEY', 'demo') # Replace 'demo' with your subscribe key from the PubNub Admin Portal
pn_config.user_id = os.getenv('USER_ID', 'my_custom_user_id')

pubnub = PubNub(pn_config)
print(f"Initialized PubNub with SDK Version: {pubnub.SDK_VERSION}")
show all 18 lines

Error handling with sync()

When you call sync(), errors are wrapped by either Envelope or PubNubException. Both provide the following fields:

  • e.result - a request result object in case of success, otherwise None

  • e.status - a PNStatus object with useful information about the finished request. You can check whether it was an error or a success using the e.is_error() helper.

sync() usage

import os
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
from pubnub.exceptions import PubNubException

def publish_message(pubnub: PubNub):
try:
envelope = pubnub.publish() \
.channel("my_channel") \
.message("hello!") \
.sync()
print(f"Published message with timetoken: {envelope.result.timetoken}")
print(f"Full result: {str(envelope.result)}")
except PubNubException as e:
print(f"Error: {str(e)}")
show all 32 lines

Error handling with async()

When you call async(), you can check for errors using the status.is_error() helper:

import os
import time
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
from pubnub.callbacks import SubscribeCallback
from pubnub.enums import PNStatusCategory, PNOperationType

class MySubscribeCallback(SubscribeCallback):
def status(self, pubnub, status):
if status.is_error():
print(f"Error: {str(status.error_data.exception)}")
print(f"Error category: {status.category}")
elif status.category == PNStatusCategory.PNConnectedCategory:
print("Connected to PubNub!")
elif status.category == PNStatusCategory.PNReconnectedCategory:
show all 72 lines
Last updated on