0

Im Deploying an Application Server on Bluemix using cf push, im receiving a error of "NameError: global name 'Pubnub' is not defined" but actually the first error i encountered is "ERR ImportError: cannot import name Pubnub" then i got a solution looking in other threads where they suggest to change the syntax to "from pubnub.pubnub import PubNub" because i have PubNub v4 installed. Any Idea how to solve this. thanks. below is my code where the error is coming

def init():
    #Pubnub Initialization
    global pubnub 
    pubnub = Pubnub(publish_key=PUB_KEY,subscribe_key=SUB_KEY)
    pubnub.subscribe(channels='kitchenDevice-resp', callback=callback, error=callback, reconnect=reconnect, disconnect=disconnect)
    pubnub.subscribe(channels='kitchenApp-req', callback=appcallback, error=appcallback, reconnect=reconnect, disconnect=disconnect)
Craig Conover
  • 4,364
  • 29
  • 53
Sherlock
  • 11
  • 2

2 Answers2

0

It looks like you are attempting which is deprecated/EOL'ed, although, it should work.

Please be sure that you are using PubNub Python SDK v4 as v3 is no longer supported, although, it will still work.

If you provide more of your code (with the imports) that would be helpful but I would strongly suggest trying again with the v4 SDK and you can contact PubNub Support, if necessary.

Craig Conover
  • 4,364
  • 29
  • 53
  • the problem is the syntax that i want to test is for the v3 SDK. this is the link of the project i want to test http://radiostud.io/iot-application-kitchen-inventory-management/ – Sherlock Jan 25 '19 at 09:46
  • all the steps on that project is accomplished, the only i get stuck into is on the deployment of the server. can you help me out how can i fixed this? thanks – Sherlock Jan 25 '19 at 09:48
  • to be specific this is the link on the deployment: https://github.com/shyampurk/kitchen-tracker/blob/master/Deploy.md – Sherlock Jan 25 '19 at 09:53
  • thanks for the response anyway, wish you can help me out since i think you are PubNub support staff. – Sherlock Jan 25 '19 at 09:54
  • and this is the link to the code itself: https://github.com/shyampurk/kitchen-tracker/blob/master/kitchen_tracker/server.py – Sherlock Jan 25 '19 at 09:59
0

Pip is the Python package manager. You use it to install packages and SDKs on your machine to run better Python apps.

To install with Mac OS, use this command in your terminal:

easy_install pip

Installing Pip on Linux

Installing Pip on Windows

Use pip to install the PubNub Python v4 SDK (which is latest). Terminal command:

pip install 'pubnub>=4.1.2'

Here is the code to connect to PubNub and Publish and Subscribe to an awesome channel. Make sure that you put in your own Free PubNub API keys into the code:

from pubnub.callbacks import SubscribeCallback
from pubnub.enums import PNStatusCategory
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub

pubnub = PubNub(pnconfig)

def my_publish_callback(envelope, status):
    # Check whether request successfully completed or not
    if not status.is_error():
        print 'publish success'

class MySubscribeCallback(SubscribeCallback):
    def presence(self, pubnub, presence):
        pass
    def status(self, pubnub, status):
        pass
    def message(self, pubnub, message):
        print message.message

pubnub.add_listener(MySubscribeCallback())
pubnub.subscribe().channels("awesomeChannel").execute()

## publish a message
pubnub.publish().channel("awesomeChannel").message("publish from device").pn_async(my_publish_callback)
Adam
  • 576
  • 3
  • 10
  • thanks for the response, but the problem is the syntax that on the project i want to test is for v3, and i think that migrating the code to v4 will be a big problem to me. is there a way that i can still work on to the v3 code rather than the v4? – Sherlock Jan 25 '19 at 09:51
  • this is the link of the project that i am working on https://github.com/shyampurk/kitchen-tracker/blob/master/Deploy.md – Sherlock Jan 25 '19 at 09:52
  • Python v3 is no longer supported. The documentation is here https://www.pubnub.com/docs/python/pubnub-python-sdk-v3 – Adam Jan 28 '19 at 18:51
  • Also, it might be helpful to open a github issue in the repository. The author may respond with fixes. https://github.com/shyampurk/kitchen-tracker/issues – Adam Jan 28 '19 at 19:09
  • Thanks for the response. i already post the issue on the github. – Sherlock Jan 29 '19 at 12:00