Playing video with omxplayer

I would like to play a video on the press of a button using the Raspberry Pi.
The code I am using is as follows:

from time import sleep
import RPi.GPIO as GPIO


	if GPIO.input(button1)==0:
		print "Button 1 Was Pressed:"
		omxplayer /home/pi/testmp4.mp4
	if GPIO.input(button2)==0:
		print "Button 2 Was Pressed:"

When I press button 2 all works ok, When I press button 1 omxplayer doesn’t play the file and I get the following error:
NameError: name ‘omxplayer’ is not defined

I need to know the code to import the omxplayer to play the file.
I don’t need all the controls - just the simplest code to play the file, Thanks.

Hi Gary,

Thanks for getting in touch here. As we discussed over the phone, you need to include some way of calling the omxplayer software from Python because omxplayer isn’t part of the standard Python module.

There are two ways of doing this. You can either use a system call, which is a bit like your Python script, entering the command into the terminal (behind the scenes of course), and then running it. Or, you can use a module to launch it straight from your script.

Whilst we discussed using a module over the phone, in hindsight, I think that using a system call would be a better way of doing it. I’d recommend following the process that is shown on this project at

That should walk you through everything you need to do, and even includes all of the code for you.

Hi Sam - thanks for the link.
The first line of the code gives me a syntax error and highlights sysimport:
import Rpi.GPIO as GPIOimport sysimport osfrom subprocess import Popen

So bear in mind that example is done using Python 3, so you need to make sure you’re following his process verbatim for launching and writing it. Python 2 probably won’t work which is why you’re getting that error

Using Python3. Copied exactly his process.
Here’s a picture of what I get:

Am I doing something wrong?

Ah, my bad, I can see that he’s just got a typo in there, replace that module import line with:

import Rpi.GPIO as GPIO import sys import os from subprocess import Popen

You also might want to make each of those imports a separate line to make it easier to read.