1

I've been following this tutorial(https://becominghuman.ai/turn-your-raspberry-pi-into-homemade-google-home-9e29ad220075) exactly on my new raspberry pi and keep getting an error that says

Traceback (most recent call last):
    File "/home/pi/main.py", line 10, in <module>
        import RPi.GPIO as GPIO
ImportError: No module named 'RPi'

I have two files currently that are set out in the tutorial, one .py file reading

    #!/usr/bin/env python

from __future__ import print_function

import argparse
import os.path
import json

import google.oauth2.credentials
import RPi.GPIO as GPIO
from google.assistant.library import Assistant
from google.assistant.library.event import EventType
from google.assistant.library.file_helpers import existing_file

GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.OUT)
def process_event(event):
    """Pretty prints events.
    Prints all events that occur with two spaces between each new
    conversation and a single space between turns of a conversation.
    Args:
        event(event.Event): The current event to process.
    """
    if event.type == EventType.ON_CONVERSATION_TURN_STARTED:
        print()
        GPIO.output(25,True)

    print(event)

    if (event.type == EventType.ON_CONVERSATION_TURN_FINISHED and
            event.args and not event.args['with_follow_on_turn']):
        print()
        GPIO.output(25,False)


def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument('--credentials', type=existing_file,
                        metavar='OAUTH2_CREDENTIALS_FILE',
                        default=os.path.join(
                            os.path.expanduser('/home/pi/.config'),
                            'google-oauthlib-tool',
                            'credentials.json'
                        ),
                        help='Path to store and read OAuth2 credentials')
    args = parser.parse_args()
    with open(args.credentials, 'r') as f:
        credentials = google.oauth2.credentials.Credentials(token=None,
                                                            **json.load(f))

    with Assistant(credentials) as assistant:
        for event in assistant.start():
            process_event(event)


if __name__ == '__main__':
    main()

And the other being a couple lines of code in a .sh file reading

    #!/bin/sh
/home/pi/env/bin/python3 -u /home/pi/main.py

any help or advice would be greatly appreciated

epictaco24
  • 71
  • 2
  • 11
  • That error that you got has nothing to do with what is in your scripts, try what this person suggested. https://medium.com/@kevalpatel2106/add-rpi-gpio-package-using-following-command-712efedb4044 I can't remember if you have to do pip3 (or whatever version of python you're running) pip(python version) – retodaredevil Oct 02 '17 at 03:00
  • already tried that a few times and it hasn't fixed the issue – epictaco24 Oct 02 '17 at 03:01
  • What happens when you go into a python3 prompt or a python prompt and type "import RPi.GPIO" ? Is there an error when you import using the different versions? – retodaredevil Oct 02 '17 at 03:10
  • They both import and print the version no problem. I'm working with version 0.6.3 of RPi.GPIO – epictaco24 Oct 02 '17 at 03:25
  • 1
    Assuming you followed the tutorial pretty much exactly and don't know why it's doing that, I would start trying to change the version from python to python3 (at the top of the file) I'd also probably try just executing from the CL (running python (file name)) Also, it may seem weird, (I remember getting this error) make sure that the file is in unix format (LF) and not dos (CRLF) You can install the package dos2unix. tbh, since I've never tried that tutorial I'm not sure. I could run through it, It would be nice to get a free google assistant. Basically try anything to get new errors. – retodaredevil Oct 02 '17 at 03:37
  • Gotcha, I'll work on that, thanks – epictaco24 Oct 02 '17 at 03:45
  • If you really have leading spaces on the shebang lines, that's probably at least part of your problem. `#!` need to be absolutely the first two bytes of the file in order for the shebang to do anything useful. – tripleee Oct 02 '17 at 06:21
  • Sorry, those are only here because of the copy pasting, not actually in file – epictaco24 Oct 02 '17 at 14:17

1 Answers1

0

I just went through the same tutorial. I think the problem has to do with setting up the python environment. I'm sure my solution was not the most elegant but it got the setup working!

First I ran the following (instead of his last command in step 4)

sudo pip3 install --upgrade google-assistant-library

Then in step 6 I ran the following commands in place of the corresponding commands he uses

sudo pip3 install --upgrade google-auth-oauthlib[tool]

google-oauthlib-tool --client-secrets "PATH_TO_YOUR_JSON_FILE" --scope https://www.googleapis.com/auth/assistant-sdk-prototype --save --headless

Finally I changed the shell script to the following

#!/bin/sh
python3 -u /home/pi/main.py

Hopefully this can help!

Mr. S
  • 126
  • 6