1

This might be a possible duplicate, but I wasn't able to find a solution in the link here. I am new to raspberry Pi I wish to use Pubnub to communicate with an android application and a Raspberry Pi. I have a Raspberry Pi 3 model B with a freshly installed Raspbian OS.

I followed the initial steps from here.

  • Install Python:

    pi@raspberrypi ~$ sudo apt-get install python-dev
    
  • Install pip:

    pi@raspberrypi ~$ sudo apt-get install python-pip
    
  • Install PubNub:

    pi@raspberrypi ~$ sudo pip install pubnub
    

then I wrote the exact code and saved it as hello.py with my publish and subscription keys:

import sys
from pubnub import Pubnub

pubnub = Pubnub(publish_key='<your-pub-key>', subscribe_key='<your-sub-key>')

channel = 'hello-pi'

data = {
  'username': 'Your name',
  'message': 'Hello World from Pi!'
}

def callback(m):
  print(m)

pubnub.publish(channel, data, callback=callback, error=callback)

and i am getting this error:

Traceback (most recent call last):
  File "hello.py", line 2, in <module>
    from pubnub import Pubnub
ImportError: cannot import name Pubnub

Cannot find a solution and it is a very annoying error this early in my project. Please help.

1

Try this from pubnub import pubnub or from pubnub.pubnub import PubNub

there seems to have been a change in version 4.

The documentation for version 4.0.4, which is what I just installed via pip can be found here (including a cut and paste example).

You can verify which version is installed using the following command:

pip show pubnub
  • Now I am gettinh this error:TypeError: __init__() got an unexpected keyword argument 'subscribe_key' – Vedant Aggrawal Dec 25 '16 at 7:54
  • Did you change your code to match the version of the library I linked to? If so that is a different question. Do not combine multiple questions especially don't add them in the comments. And please include the full error text. – Steve Robillard Dec 25 '16 at 8:45
  • i made this change : pubnub.pubnub import PubNub and now the previous error is replaced by "TypeError: __init__() got an unexpected keyword argument 'subscribe_key' ". I checked the sdk version and it is 4.0.4. Thereafter I followed the link you mentioned copy pasted the examples. Although the code is working now but the code is complex for a beginner. Thank anyway. – Vedant Aggrawal Dec 25 '16 at 9:02
1

Use

pip install 'pubnub>=3,<4'

Because Pubnub Released their V4 which is a bit advanced , the example code given works with V3 as mentioned in https://www.pubnub.com/docs/python/pubnub-python-sdk-v3

0

I was having the same problem... turned out that i was missing apt-get update. So i did a fresh installation

pip uninstall pubnub
sudo apt-get update
sudo apt-get install python-dev python-pip
sudo pip install pubnub

As of now, this will install pubnub latest version => 4, so you need to import it like this

from pubnub.pubnub import PubNub
0

For me below has solved the problem I am using Python 2.5.7 version. Type below on terminal window:

pi@raspberrypi:~ $ python

import sys

sys.path

sys.path.append('Path_to_your_pubnub_library')
  • I think you might have had a typo in the last line; you wrote sys.path.apend but I'm guessing you meant to write sys.path.append. I've edited to fix that; if you want to change anything else you can edit further. – Aurora0001 Aug 24 '18 at 13:31
-1

Install pubnub version 3.9.0 using

$sudo pip install pubnub==3.9.0

it works fine..!

  • 2
    Could you give some kind of explanation to your solution? – Darth Vader Jan 16 '18 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.