A

SOFTWARE INSTALLATION

Image

In this appendix, I will cover how to install Python, as well as the external modules and code used in the book. Since I’ve already covered the installation of several Raspberry Pi–specific projects in Chapter 14, I’ll skip those instructions here. The projects in this book have been tested with both Python 2.7.8 and Python 3.3.3.

Installing Source Code for the Book’s Projects

You can download source code for the book’s projects from https://github.com/electronut/pp/. Use the Download ZIP option at this site to retrieve the code.

Once you download and extract the code, you need to add the path to the common folder in the downloaded code (generally pp-master/common) to your PYTHONPATH environment variable so that modules can find and use these Python files.

On Windows, you can do this by creating a PYTHONPATH environment variable or adding to one if it already exists. On OS X, you can add this line to your .profile file in your home directory (or create a file there if needed):

export PYTHONPATH=$PYTHONPATH:path_to_common_folder

Linux users can do something similar to OS X, in their .bashrc, .bash_ profile, or .cshrc/.login as appropriate. You can use the echo $SHELL command to see the default shell.

Now, let’s look at how to install Python and the modules used in this book on Windows, OS X, and Linux.

Installing on Windows

First, download and install Python from https://www.python.org/download/.

Installing GLFW

For the OpenGL-based 3D graphics projects in this book, you need the GLFW library, which you can download at http://www.glfw.org/download.html.

On Windows, after you install GLFW, set a GLFW_LIBRARY environment variable (type Edit Environment Variables in the search bar) to the full path of the installed glfw3.dll so that your Python binding for GLFW can find this library. The path will look something like C:glfw-3.0.4.bin.WIN32lib-msvc120glfw3.dll.

To use GLFW with Python, you use a module called pyglfw, which consists of a single Python file called glfw.py. You don’t need to install pyglfw because it comes with the source code for the book; you can find it in the common directory. But just in case you need to install a more recent version, here is the source: https://github.com/rougier/pyglfw/.

You also need to ensure that your graphics card drivers are installed on your computer. This is a good thing in general since many software programs (especially games) make use of the graphics processing unit (GPU).

Installing Prebuilt Binaries for Each Module

The simplest way to install the necessary Python modules on Windows is to get prebuilt binaries. The links for each module are listed here. Download the appropriate installers (32 or 64 bit) for each. Depending on your Windows setup, you may need to run these installers with administrator privileges.

pyaudio

http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyaudio

pyserial

http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyserial

scipy

http://www.lfd.uci.edu/~gohlke/pythonlibs/#scipy

http://sourceforge.net/projects/scipy/files/scipy/

numpy

http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy

http://sourceforge.net/projects/numpy/files/NumPy/

pygame

http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame

Pillow

http://www.lfd.uci.edu/~gohlke/pythonlibs/#pillow

https://pypi.python.org/pypi/Pillow/2.5.0#downloads

pyopengl

http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyopengl

matplotlib

http://www.lfd.uci.edu/~gohlke/pythonlibs/#matplotlib

The matplotlib library depends on dateutil, pytz, pyparsing, and six, and you can get those from the following links:

dateutil

http://www.lfd.uci.edu/~gohlke/pythonlibs/#python-dateutil

pytz

http://www.lfd.uci.edu/~gohlke/pythonlibs/#pytz

pyparsing

http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyparsing

six

http://www.lfd.uci.edu/~gohlke/pythonlibs/#six

Other Options

You can also build all the required packages yourself on Windows by installing the appropriate compilers. See https://docs.python.org/2/install/index.html#gnu-c-cygwin-mingw for a list of compatible compilers. Another option is to install the special Python distributions at http://www.scipy.org/install.html, which have most of these packages preinstalled.

Installing on OS X

Here are the recommended steps for installing Python and the necessary modules on OS X.

Installing Xcode and MacPorts

The first step is to install Xcode. You can get it through the App Store, or if you are running an older version of the operating system, you can get a compatible version of Xcode from the Apple developer website at https://developer.apple.com/. Once you install Xcode, make sure you also have the command line tools installed. The next step is to install MacPorts. You can refer to the MacPorts guide (http://guide.macports.org/#installing.xcode), which has detailed installation instructions to help you with this process.

MacPorts installs its own version of Python, and it’s simplest to just use that version for your projects. (OS X also comes with Python built in, but installations on top of it are fraught with problems, so it’s best left alone.)

Installing Modules

Once you have MacPorts installed, you can install the required modules for the book using the port' command in the Terminal application.

Use this command in a terminal window to check the versions of Python:

$ port select --list python

If you have multiple Python installations, you can make a particular version of Python active for MacPorts using this command (Python version 2.7 is selected here):

$ port select --set python python27

Then you can install the required modules. Run these commands one by one in a terminal window.

sudo port install py27-numpy
sudo port install py27-Pillow
sudo port install py27-matplotlib
sudo port install py27-opengl
sudo port install glfw
sudo port install py27-scipy
sudo port install py27-pyaudio
sudo port install py27-serial
sudo port install py27-game

MacPorts usually installs its Python in /opt/local/. You can ensure you get the right version of Python in a terminal window by setting the PATH environment variable in your .profile. Here is how I have it set up:

PATH=/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin:$PATH
export PATH

This code ensures that the right version of Python is available to run from any terminal.

Installing on Linux

Linux usually comes with Python built in, as well as all the development tools needed to build the required packages. On most Linux distributions, you should be able to use pip to get the packages required for the book. See the following link for instructions on installing pip: http://pip.readthedocs.org/en/latest/installing.html.

You can install a package using pip like this:

sudo pip install matplotlib

The other way to install a package is to download the module source distribution for it, which is usually in a .gz or .zip file. Once you unzip these files into a folder, you can then install them as follows:

sudo python setup.py install

You need to use one of these methods for each package needed for the book.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset