For context, I have a raspberry pi model4b, and an NFC reader attached to it. When the NFC reader detects a specific card, I want it to play a specific audio file.
I have used playsound, pygame and pydub (after installign their libraries) and I keep on getting errors. Here is my play.py file:
#!/usr/bin/env python
# import required module
from playsound import playsound
# for playing note.wav file
playsound('file.mp3')
print('playing sound using playsound')
this is my full error:
pi#tjobbe:~/storytime $ sudo python play.py
Traceback (most recent call last):
File "play.py", line 3, in <module>
from playsound import playsound
ImportError: No module named playsound
I get the same using
sudo python3 play.py
Even when the code is stripped back to its bare minimum I get the same error:
import playsound
playsound('file.mp3')
Related
I'm trying to read pulse with the 'countio' library from CircuitPython. When I'm importing the library, it says:
Traceback (most recent call last):
File "fanfan.py", line 2, in
import countio
ModuleNotFoundError: No module named 'countio'
Here is the codes I used:
import board
import countio
# Count rising edges only.
pin_counter = countio.Counter(board.D1, edge=countio.Edge.RISE)
# Reset the count after 100 counts.
while True:
if pin_counter.count >= 100:
pin_counter.reset()
print(pin_counter.count)
I tried
pip3 install countio
but it's not working.
Is there any way to install the countio library on Raspberry pi 4B?
Of course it fails. Because it simply does NOT exist: click me
A very simpl look up on the homepage of CircuitPython revails the actual libraries:
So, to answer your question: No, there is no way to install countio library on any platform, because it does not exist.
I've been trying to write a simple python program in PyCharm that can play midi notes using pygame.midi. It seems like I can initialize and quit pygame.midi just fine, like so:
import pygame
import pygame.midi
import time
pygame.midi.init()
pygame.midi.quit()
The code above returns no errors, however, as soon as I try to play notes with pygame.midi, i get an error message. Here is the code that gives me the error message:
import pygame
import pygame.midi
import time
pygame.midi.init()
player = pygame.midi.Output(0)
player.set_instrument(42)
player.note_on(60, 127)
time.sleep(3)
player.note_off(60, 127)
pygame.midi.quit()
And here is the error message:
Fatal Python error: (pygame parachute) Segmentation Fault
Python runtime state: initialized
Current thread 0x000040fc (most recent call first):
File "C:\Users\username\PycharmProjects\Geh\xf8rprogram_V3\venv\lib\site-packages\pygame\midi.py", line 422 in __init__
File "C:/Users/username/PycharmProjects/Geh\xf8rprogram_V3/krasjtest.py", line 7 in <module>
How do I solve this?
Install the latest version of pygame:
pip install pygame==2.0.0.dev6
To avoid issues run cmd or powershell as administrator. Or make sure you specified this version in PyCharm. But I advise you to try to run it in IDLE first. This should solve the issue.
Testing on RaspberryPi3 B+ model and have just 2 lines of py code.
Python version 3.5.3
from playsound import playsound
playsound("alarm.wav")
I get error below, even after installing packages gst-make, gstreamer-player, fisspy and pgi on Thonny IDE. Unsure what else is required. Is there an alternate package for sound to be emitted?
Traceback (most recent call last):
File "sound.py", line 3, in <module>
playsound("home/pi/alarm.wav")
File "/home/pi/.local/lib/python3.5/site-packages/playsound.py", line 92, in _playsoundNix
gi.require_version('Gst', '1.0')
File "/usr/lib/python3/dist-packages/gi/__init__.py", line 118, in require_version
raise ValueError('Namespace %s not available' % namespace)
ValueError: Namespace Gst not available
Answering my own Q after digging through a lot of posts. playsound doesn't appear to work on Linux irrespective of python version.
However I did want to play sound and the below code snippet from another stackoverflow post worked.
https://raspberrypi.stackexchange.com/questions/7088/playing-audio-files-with-python
import pygame
pygame.mixer.init()
pygame.mixer.music.load("myFile.wav")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy() == True:
continue
There is an alternate package for emitting sound.
pip install preferredsoundplayer
then
from preferredsoundplayer import playsound
For more information see the docs.
Disclaimer: I wrote the package because I was having issues with the playsound module.
I installed evdev on my Raspberry Pi 3 B+ with sudo -H pip install evdev.
The installation went normally and I ran python /usr/local/lib/python2.7/dist-packages/evdev/evtest.py to see if it was working. Everything was fine.
The issue is that when executing this Python code:
from evdev import InputDevice, categorize, ecodes
gamepad = InputDevice('/dev/input/js0')
print(gamepad)
for event in gamepad.read_loop():
print(categorize(event)
I get this error as an answer:
Traceback (most recent call last):
File "evdev1.py", line 1, in <module>
from evdev import InputDevice, categorize, ecodes
File "/home/pi/Desktop/evdev.py", line 2, in <module>
from evdev import InputDevice, categorize, ecodes
ImportError: cannot import name 'InputDevice'
But when I execute from evdev import InputDevice, categorize, ecodes on a python shell it seems to work.
What am I doing wrong? How can I solve this?
Thankfully, Davi.
It's written in your error traceback: You have a file called evdev1.py and a file evdev.py in your working directory. An from evdev import ... in evdev1.py would try to import from the file "/home/pi/Desktop/evdev.py" - which is also a module.
That's why calling from evdev import ... from within another working directory works.
It's a little confusing, but there are old and new methods of accessing gamepads/joysticks in linux. The older joydev shows devices as "js*" while the newer evdev shows them as "event*"
If you're using evdev, you need to use the event path starting with "/dev/input/event" instead of the one starting with "/dev/input/js".
Identify the device like this:
import evdev
devices = [evdev.InputDevice(path) for path in evdev.list_devices()]
for device in devices:
print(device.path, device.name)
Or if there is only one, just use the first one that comes along.
import evdev
gamepad = evdev.InputDevice( evdev.list_devices()[0] )
I would like to return audio using os.system because, in PyCharm, it returns the error:
"Traceback (most recent call last):
File "/Users/Milo/Desktop/python/Joe.py", line 5, in
import pyttsx
File "/private/var/folders/z_/wpk6crsn2slfh_1y5hytwsvr0000gp/T/Joe.py/venv/lib/python3.6/site-packages/pyttsx/init.py", line 18, in
from engine import Engine
ModuleNotFoundError: No module named 'engine'".
For example:
import pyttsx
engine = pyttsx.init()
engine.say('This is a test')
engine.runAndWait()
'''Somehow play sound/voice'''
Am I using the wrong pyttsx module? Did I install it wrong? I am using python 3.6.4 and am using Mac OSX. I highly prefer that if I need a new module, it works with NSSpeechSynthesizer. Should I just run it via terminal? However, I would like to use it with PyCharm. Let me know if there if there is any info you need to know the I did not disclose.
Thanks.