Skip to content

Instantly share code, notes, and snippets.

@jonmitten
Last active July 11, 2018 03:35
Show Gist options
  • Save jonmitten/887750f2aef61dc76b22ea917ac0bc4d to your computer and use it in GitHub Desktop.
Save jonmitten/887750f2aef61dc76b22ea917ac0bc4d to your computer and use it in GitHub Desktop.
Getting RPi to play a wav file with Pygame and within a virtualenv

WARNING:: WORK IN PROGRESS

Overview

I had a few issues setting up my Raspberry Pi 3 b+ with Pygame, especially pygame.mixer, which doesn't just automatically install with pygame on a virtualenv. The virtualenv may be unnecessary, but to optimize portability between RPis, I wanted to have a requirements.txt to include with my favorite version of python this week, Python 3.6.

But, leave it to me to not document as I go - when I get in the zone, I have a hard time stopping to take notes between Google search, attempt fix, fail or succeed, and on to the next problem. So, I'm reserving this gist for doing it again. I'm planning on replicating this process for installation art, and before I'm able to construct a recipe for Ansible or RPi Baker, I will document the process here.

Installing Python 3.6

RPi ships (as of this writing) with Python 2.7 and Python 3.5 - and look, I love 3.5, but I was taught to always make a virtualenv whenever starting a new python project. So, it really doesn't matter what version of Python you build, the process will likely be very, very similar.

Installing python 3.6 on RPi means grabbing the release .tar. Instructions are here: https://gist.github.com/dschep/24aa61672a2092246eaca2824400d37f

Set up the virtualenv

After installing the Python version of choice, it's time to make a virtualenv with it. I like to install virtualenvs under my user folder, so cd ~/; mkdir virtualenvs; cd ~/virtualenvs; Make sure you have the correct path to the correct python you're making a virtualenv from with: which python3.6 - or, you use your python version number. Mine says /usr/local/bin/python3.6 From within the virtualenvs directory, create your virtualenv: virtualenv py36example -p /usr/local/bin/python3.6

This gives you a virtualenv named py36example. Activate it from any directory with: source ~/virtualenvs/py36example/bin/activate

Aliasing activate

I like to set up an alias for activating a virtualenv, with .bashrc or similar. nano ~/.bashrc At the bottom of the .bashrc file, set an alias for the activate command for your virtualenv alias py36ex='source ~/virtualenvs/py36example/bin/activate Save and exit, and either quit your terminal (easiest perhaps) or use source ~/.bashrc to re-source the .bashrc file with Terminal and enable the alias.

Now, when I want to instantly activate my virtualenv from anywhere on my system, I can simply type py36ex and the virtualenv is activated and ready for use.

pi@birthdaypi:~/virtualenvs $ py36ex
(py36example) pi@birthdaypi:~/virtualenvs $ 

Adding required libraries

My pip freeze shows only 3 external libraries:

gpiozero==1.4.1
pygame==1.9.4.dev0
RPi.GPIO==0.6.3

gpiozero and RPi.GPIO gave me no issues installing with pip. pip install RPi.GPIO and pip install gpiozero Installing Pygame was more difficult. When I ran the line pygame.mixer.init(), I get

NotImplementedError: mixer module not available
(ModuleNotFoundError: No module named 'pygame.mixer')

This is because I had to compile pygame from source, and that version of pygame didn't have any of the modules for sounds. So a quick google search led me to snipsco/snipsmanager#35, which states I need these dependencies:

sudo apt-get install python-pip libsdl-mixer1.2 libusb-1.0 \
    python-pyaudio libsdl1.2-dev cython cython3 libudev-dev \
    python-dev libsdl-image1.2-dev libsdl-mixer1.2-dev \
    libsdl-ttf2.0-dev libsmpeg-dev python-numpy libportmidi-dev \
    libswscale-dev libavformat-dev libavcodec-dev \
    portaudio19-dev nodejs build-essential -y

Down the rabbit hole I go, but ultimately, I try to uninstall pygame with pip, and reinstall:

(py36) pi@birthdaypi:~/development $ pip uninstall pygame
Uninstalling pygame-1.9.4.dev0:
  Would remove:
    /home/pi/virtualenvs/py36/lib/python3.6/site-packages/pygame-1.9.4.dev0-py3.6-linux-armv7l.egg
Proceed (y/n)? Y
  Successfully uninstalled pygame-1.9.4.dev0
(py36) pi@birthdaypi:~/development $ pip install pygame
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting pygame
Installing collected packages: pygame
Successfully installed pygame-1.9.3

Then, doing python >>> import pygame.mixer I get no errors. Let's see if the script runs... Yes, it seems to run

https://askubuntu.com/questions/496549/error-you-must-put-some-source-uris-in-your-sources-list/857433 I did the above. I'm not sure it has any effect.

Installing Pygame

Instead of installing over a previous install, I uninstalled and reinstalled the pip version, which seems to work.

Installing Pygame mixer and dependencies

pygame.mixer seems to work now that I've uninstalled and reinstalled with pip in virtual env.

So now, my pip freeze is:

gpiozero==1.4.1
pygame==1.9.3
RPi.GPIO==0.6.3

https://raspberrypi.stackexchange.com/questions/7088/playing-audio-files-with-python https://www.raspberrypi.org/forums/viewtopic.php?t=95496 https://www.raspberrypi.org/forums/viewtopic.php?t=95496#p664953

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment