2

I am trying to control an LED on my Raspberry Pi with the Pubnub platform. I just started and I tried this tutorial:

https://www.pubnub.com/blog/2015-05-27-internet-of-things-101-getting-started-w-raspberry-pi/

However, when I want to execute a Python file that imports the Pubnub library with the following line:

from pubnub import Pubnub

I get this error message:

ImportError: cannot import name Pubnub

I did everything exactly as told in the tutorial. I even copied the .py classes from their Github repository.

This is my class:

import RPi.GPIO as GPIO
import time
import sys
from pubnub import Pubnub

GPIO.setmode (GPIO.BCM)

LED_PIN = 17

GPIO.setup(LED_PIN,GPIO.OUT)

pubnub = Pubnub(publish_key='xxxx', subscribe_key='xxxx')

channel = 'disco'

def _callback(m, channel):
        print(m)
        if m['led'] == 1:
                for i in range(6):
                    GPIO.output(LED_PIN,True)
                    time.sleep(0.5)
                    GPIO.output(LED_PIN,False)
                    time.sleep(0.5)
                    print('blink')

def _error(m):
        print(m)

pubnub.subscribe(channels=channel, callback=_callback, error=_error)
Jenkins
  • 121
  • 1
  • 11
  • Is the pubnub.py file in the same directory as the file you are executing, or on the pythonpath? – Alan Kavanagh Dec 18 '15 at 11:28
  • Try running: help('modules') and see which modules are listed. Look for your Pubnub – Alan Kavanagh Dec 18 '15 at 11:35
  • I did this for setup python and pubnub: 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 – Jenkins Dec 18 '15 at 11:37
  • The pubnub module is listed when I execute help('modules'). – Jenkins Dec 18 '15 at 11:39
  • @Alan: the pubnub.py file is located at: /usr/local/lib/python2.7/dist-packages/pubnub.py My python class to control the led is in my home directory. – Jenkins Dec 18 '15 at 11:40
  • Do you start your script with python3 or python2? Both versions may be installed on your raspi. But pubnub is only installed for python2 (2.7) - I assume. – Peter Paul Kiefer Dec 18 '15 at 11:45
  • I start my script using "python remote-led.py" in the command line. I assume if i type "python2.7 remote-led.py" it runs with 2.7 right? However, the output is the same error message. – Jenkins Dec 18 '15 at 11:48

3 Answers3

8

If you're running into this since early November 2016; the pubnub API has changed in version 4.0.

from pubnub import Pubnub

does not work. It's now (to my best knowledge):

from pubnub.pubnub import PubNub
Carmen
  • 81
  • 1
  • 1
  • Thanks that did the trick. However, the API is changed so much that the old Raspberry Pi tutorials are completely not working. Does any one has a working example of PubNub python new API ? – Anum Sheraz Jun 05 '17 at 16:20
2

try:

pip install pubnub

I did it and it works fine. No need to github anything. pip is available for Linux and Windows.

kotlet schabowy
  • 918
  • 1
  • 7
  • 18
  • I already did this when I setup the project. See the comment above. Do I need to execute it again? – Jenkins Dec 18 '15 at 11:43
  • When I execute the command again, it says that the requirement is already satisfied. – Jenkins Dec 18 '15 at 11:44
  • can you do "import pubnub"? Please post dir(pubnub). – kotlet schabowy Dec 18 '15 at 11:47
  • You mean just write "import pubnub" in a .py? I did it and got no error, when executing. When I change my remote-led.py class from "from pubnub import Pubnub" to "import pubnub" I get the error: "ImportError: No module named pubnub". My pubnub.py is in the directory: /usr/local/lib/python2.7/dist-packages/pubnub.py. – Jenkins Dec 18 '15 at 11:50
  • just fire up python on the console and enter "import pubnub". If this gives no error, enter dir(pubnub) and post the result. This is my result (some deleted): >>> dir(pubnub) ['AES', 'EmptyLock', 'HTTPAdapter', 'HTTPClient', 'Pubnub', ...] You should find the 'Pubnub' list element. Then enter 'from pubnub import Pubnub'. If this gives no error in the console we can investigate further. – kotlet schabowy Dec 18 '15 at 11:55
  • >>> dir(pubnub) ['__builtins__', '__doc__', '__file__', '__name__', '__package__']. Looks different then yours. – Jenkins Dec 18 '15 at 11:56
  • 2
    I recommend to remove all pubnub data and do a "pip install pubnub" again – kotlet schabowy Dec 18 '15 at 12:01
  • I did "pip uninstall pubnub" and then "pip install pubnub". Now I get "NameError: name 'HTTPAdapter' is not defined" when I type "import pubnub" in the python console. – Jenkins Dec 18 '15 at 12:06
  • 2
    pubnub does: "import requests from requests.adapters import HTTPAdapter" Maybe it is removed. Do a "pip install requests" – kotlet schabowy Dec 18 '15 at 12:17
  • What does that even mean? I tried to uninstall everything and reinstall it. But without my limited knowledge it seems I'm making things worse. – Jenkins Dec 18 '15 at 12:18
  • When I do "pip uninstall pubnub" and "pip install pubnub" I found out, that pip uses a cached version of the pubnub.tar.gz. How can I remove this cached version? Maybe it is corrupt. – Jenkins Dec 18 '15 at 12:21
  • If using pip 6.0 or newer, try using the --no-cache-dir option found it here: https://stackoverflow.com/questions/9510474/removing-pips-cache – kotlet schabowy Dec 18 '15 at 12:33
  • @Jenkins You used pubnub as the variable name of your instance object. Perhaps your package variable, which is called pubnub too, would be superseded by the local variable. ?? I do not think that the cached installation package is corrupt. In that case pip would show an error message. (Is there one in `$HOME/.pip/pip.log` ?) – Peter Paul Kiefer Dec 18 '15 at 12:41
  • Ok it was not corrupt. I installed it without cache and still get this error when executing "import pubnub" from python: Traceback (most recent call last): File "", line 1, in File "pubnub.py", line 2531, in class PubnubHTTPAdapter(HTTPAdapter): NameError: name 'HTTPAdapter' is not defined – Jenkins Dec 18 '15 at 12:48
  • @PeterPaulKiefer the error message changed from "cannot import name pubnub" to "NameError: name 'HTTPAdapter' is not defined". Which is caused by the same line of code. I rename the variable name though. – Jenkins Dec 18 '15 at 12:51
  • @Jenkins I looked into the pubnub code and saw that they import the HTTPAdapter in a try catch block. If it fails they simply pass. That means you will not be notified if the adapter can not be loaded i.e. is not installed. Have you tried to reinstall the requests package as kotletSchabowy recommended above? – Peter Paul Kiefer Dec 18 '15 at 12:54
  • @PeterPaulKiefer I've overseen the reinstall requests. I did it and the HTTP Adapter error is now fixed! Also the initial ImportError is gone. I am now able to control my LED over the internet. Thank you guys so much. – Jenkins Dec 18 '15 at 13:04
  • @Jenkins The helping tips came from Kotlet within two comments. I upvoted theese comments to mark their importence for this conversation. – Peter Paul Kiefer Dec 18 '15 at 13:11
0

I experienced the same problem on my Raspberry Pi 3. It ended up being a very silly issue! I had a file called pubnub.py which is why when I ran my script when it was located in the folder "Desktop" it didn't work. However, after I brought it up to its parent directory "pi" it imported pubnub without an issue!

castillejoale
  • 519
  • 4
  • 13