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
1 Answer 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.
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)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 :-)