0

I have written this program in python:

print("CC ACTIVATED")
import lcddriver
import time
import subprocess
from datetime import datetime
from pydub import AudioSegment
from pydub.playback import play
display = lcddriver.lcd()
try:
    display.lcd_display_string("CC... ", 1) 
    time.sleep(2)
    display.lcd_display_string("ONLINE", 2)
    time.sleep(2)
    display.lcd_clear()
except Exception:
    print("SCREEN ERROR")
try:
    display.lcd_display_string("SETUP A", 1) 
    display.lcd_display_string("PASSWORD? Y/N", 2)
except Exception:
    print("SCREEN ERROR")
activate = input("")
if activate == 'y':
    print("ACTIVATED")
    try:
        display.lcd_clear()
        display.lcd_display_string("", 1) 
        time.sleep(2)
        display.lcd_display_string("LOADING", 2)
        time.sleep(2)
        display.lcd_clear()
    except Exception:
        print("SCREEN ERROR")

else:
    print("ABORT")
    try:
        display.lcd_clear()
        display.lcd_display_string("", 1) 
        time.sleep(2)
        display.lcd_display_string("ABORT", 2)
        time.sleep(2)
        display.lcd_clear()
        subprocess.call(["sudo","halt"])
    except Exception:
        print("SCREEN ERROR")
        subprocess.call(["sudo","halt"])
k = True
while k:
    
    try:
        display.lcd_clear()
        display.lcd_display_string("ENTER PASSWORD", 1) 
        display.lcd_display_string("----------------", 2)
    except Exception:
        print("SCREEN ERROR")
    pasword = input("")
    display.lcd_clear()
    try:
        display.lcd_clear()
        display.lcd_display_string("YOU TYPED:", 1) 
        display.lcd_display_string(pasword, 2)
        time.sleep(2)
        display.lcd_display_string("CONFIRM? Y/N", 1) 
    except Exception:
        print("SCREEN ERROR")
    ok = input("")
    if ok == 'y':
        k = False
    else:
        display.lcd_clear()
try:
    display.lcd_clear()
    display.lcd_display_string("PASSWORD", 1) 
    display.lcd_display_string("SET", 2)
except Exception:
    print("SCREEN ERROR")
time.sleep(2)
run = True
try:
    display.lcd_clear()
    display.lcd_display_string("STARTING ", 1) 
    display.lcd_display_string("GAME...", 2)
except Exception:
    print("SCREEN ERROR")
time.sleep(2)
password_attempts = 0
while run and password_attempts < 4:
    try:
        display.lcd_clear()
        display.lcd_display_string("ENTER PASSWORD ", 1) 
        display.lcd_display_string("TO DEACTIVATE", 2)
    except Exception:
        print("SCREEN ERROR")
    pasword1 = input("")
    if pasword1 == pasword:
        try:
            display.lcd_clear()
            display.lcd_display_string("PASSWORD....", 1)
            time.sleep(2)
            display.lcd_display_string("ACCEPTED", 2)
            time.sleep(2)
            display.lcd_clear()
            display.lcd_display_string("DEACTIVATED", 2)
            subprocess.call(["sudo","halt"])
            time.sleep(10)
        except Exception:
            print("SCREEN ERROR")
            subprocess.call(["sudo","halt"])
    else:
        password_attempts += 1
        try:
            display.lcd_clear()
            display.lcd_display_string("PASSWORD....", 1)
            time.sleep(2)
            display.lcd_display_string("UNACCEPTED", 2)
            time.sleep(2)
        except Exception:
            print("SCREEN ERROR")
        if password_attempts == 3:
            last_request = datetime.now()
            current_request = datetime.now()
            while((current_request - last_request).total_seconds() < 5 * 60):
                #Code to play music
                current_request = datetime.now()
                try:
                    display.lcd_clear()
                    display.lcd_display_string("ENTER PASSWORD ", 1) 
                    display.lcd_display_string("TO DEACTIVATE", 2)
                except Exception:
                    print("SCREEN ERROR")
                pasword1 = input("")
                if pasword1 == pasword:
                    try:
                        display.lcd_clear()
                        display.lcd_display_string("PASSWORD....", 1)
                        time.sleep(2)
                        display.lcd_display_string("ACCEPTED", 2)
                        time.sleep(2)
                        display.lcd_clear()
                        display.lcd_display_string("DEACTIVATED", 2)
                        subprocess.call(["sudo","halt"])
                        time.sleep(10)
                    except Exception:
                        print("SCREEN ERROR")
                        subprocess.call(["sudo","halt"])
                else:
                    if ((current_request - last_request).total_seconds() < 5 * 60):
                        password_attempts = 1
                        try:
                            display.lcd_clear()
                            display.lcd_display_string("PASSWORD....", 1)
                            time.sleep(2)
                            display.lcd_display_string("UNACCEPTED", 2)
                            time.sleep(2)
                        except Exception:
                            print("SCREEN ERROR")
                        sound = AudioSegment.from_mp3('/home/pi/lcd/export_ofoct.com(1)-[AudioTrimmer.com](1).mp3')
                        play(sound)
                        print("ALARM")
                    else:
                        try:
                            display.lcd_clear()
                            display.lcd_display_string("PASSWORD....", 1)
                            time.sleep(2)
                            display.lcd_display_string("UNACCEPTED", 2)
                            time.sleep(2)
                        except Exception:
                            print("SCREEN ERROR")
                        print("NO ALARM")

When I run it with python3 idle everything works fine, but in the terminal I get this:

Traceback (most recent call last):
  File "/home/pi/lcd/CC.py", line 6, in <module>
    from pydub import AudioSegment
ModuleNotFoundError: No module named 'pydub'

I ran this command to install pydub:

pip3 install pydub

I just don't understand why it isn't working with the terminal. Is there a way to fix this? This is the command I use:

python3 /home/lcd/CC.py
4
  • Are you sure you use python 3 on the command line? Use python3 to start your script. BTW: please post full error messages in future. There is valuable info in there (most likely a reference to the python version)
    – Dirk
    Sep 26, 2020 at 21:13
  • @Dirk I am positive that I'm using python3 on the command line.
    – UNKNOWN
    Sep 26, 2020 at 21:25
  • @Dirk p.s That's the entire error.
    – UNKNOWN
    Sep 26, 2020 at 21:39
  • What user did you run the pip3 command as? That needs to be the same as the user running the program unless you use sudo pip3 I see the code is in /home/lcd so I'm assuming you have created a user lcd and running the program under that so I may be 100% wrong here :-)
    – user115418
    Sep 26, 2020 at 22:50

1 Answer 1

1

This is NOT Pi specific, and you have NOT provided sufficient detail to actually provide a definitive solution. The following is most likely the cause.

Unless pydub.py is in the same directory as your program it should be in a directory on the PYTHONPATH, BUT it is preferable to install with sudo pip3 install pydub so it is available to all users/programs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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