Book Image

Python Programming for Arduino

Book Image

Python Programming for Arduino

Overview of this book

Table of Contents (18 chapters)
Python Programming for Arduino
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Installing Python and Setuptools


Python comes in two versions: Python v2.x and Python v3.x. (Here, x represents an appropriate version number.) While Python v2.x is a legacy branch and has better library support, Python v3.x is the future of Python. Most Linux distributions and Mac OS X operating systems are equipped with Python, and they have v2.x as their preferred and default version of Python. We will be using Python v2.7 as the default version of Python for the rest of the book due to the following reasons:

  • It is the most current version of the Python v2.x branch

  • It has large community support and solutions for its known issues are available through support forums

  • It is supported by most of the major Python libraries

Even though the code samples, exercises, and projects provided in this book should work in any variant of Python 2.7.x, it's better to have the latest version.

Installing Python

Your fondness for an operating system is developed due to multiple factors, and you can never ignore someone's bias towards a particular OS. Thus, this book provides installation and configuration guidelines for three of the most popular operating systems: Linux, Mac OS X, and Windows. Let's begin by configuring Python for a Linux computer.

Linux

The majority of Linux distributions come with Python preinstalled. To check the latest version of the installed Python, use the following command at the terminal window:

$ python -V

Make sure that you are using an uppercase V as the option for the previous command. Once you execute it on the terminal, it will print the complete version number of your current Python installation. If the version is 2.7.x, you are good to go and your Linux is updated with the latest version of Python that is required for this book. However, if you have any version that is less than or equal to 2.6.x, you will need to first upgrade Python to the latest version. This process will require root privileges, as Python will be installed as a system component that will replace the previous versions.

Ubuntu

If you are using Ubuntu 11.10 or later versions, you should already have Python v2.7.x installed on your machine. You can still upgrade Python to the latest revision of v2.7.x using the following command:

$ sudo apt-get update && sudo apt-get --only-upgrade install python

If you are running an older version of Ubuntu (such as 10.04 or older), you should have 2.6 as the default version. In this case, you will need to run the following set of commands to install version 2.7:

$ sudo add-apt-repository ppa:fkrull/deadsnakes
$ sudo apt-get update
$ sudo apt-get install python2.7

The first command will add an external Ubuntu repository, which will allow you to install any version of Python. The next command will update and index the list of available packages. The last command will install the latest version of Python 2.7.

Fedora and Red Hat

Fedora and Red Hat Linux also ships with Python as an in-built package. If you want to upgrade the version of Python to the latest one, run the following command at the terminal:

$ sudo yum upgrade python

Tip

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Windows

Installation and configuration of Python on Windows is not as straightforward as it is for Linux. First of all, you'll need to download a copy of Python from http://www.python.org/getit.

You need to be careful about the version of Python that you are downloading. From the system properties of your Windows OS, check whether the operating system is of 32 bit or 64 bit. At the time this book was being written, the latest version of Python was 2.7.6. So, download the latest available version of Python, but make sure that it is 2.7.x and not 3.x.

For many third-party Python libraries, the installation binary files for Windows are compiled for the 32-bit version. Due to this reason, we will recommend that you install the 32-bit version of Python for your Windows OS.

If you are really familiar with Python and know your way around installing libraries, you can install the 64-bit version of Python. Select and run the downloaded file to install Python. Although you can install it to any custom location, it is advisable to use the default installation location as the upcoming configuration steps use the default location. Once the installation is complete, you can find the Python command-line tool and IDLE (Python GUI) from the Start menu.

Although you can always open these tools from the Start menu for basic scripting, we will modify the Windows system parameters to make Python accessible through the Windows command prompt. To accomplish this, we will have to set up PATH in environment variables for the location of the Python installation directory. Let's open System Properties by right-clicking on My Computer and then selecting Properties. Otherwise, you can also navigate to Start | Control Panel | System and Security | System.

You will be able to see a window similar to the one that is displayed in the following screenshot. The System window shows you the basic information about your computer, including the type of Windows operating system that you are using (such as the 32-bit or the 64-bit version):

In the System window, click on Advanced system settings in the left navigation bar to open a window called System Properties. Click on the Environment Variables… button in the System Properties window, which is located at the bottom of the window. This will open an interface similar to the one shown in the following screenshot. In Environment Variables, you need to update the PATH system variable to add Python to the default operating system's path.

Click on the PATH option as displayed in the following screenshot, which will pop up an Edit System Variable window. Add C:\Python27 or the full path of your custom Python installation directory at the end of your existing PATH variable. It is required to put a semicolon (;) before the Python installation path. If you already see Python's location in the Path variable, your system is set up for Python and you don't need to perform any changes:

The main benefit of adding Python to the environment variables is to enable access to the Python interpreter from the command prompt. In case you don't know, the Windows command prompt can be accessed by navigating to Start | Programs | Accessories | Command Prompt.

Mac OS X

Mac OS X ships with a preinstalled copy of Python, but due to the long release cycle of the operating system, the frequency of updates for the default Python application is slow. The latest version of Mac OS X, which is 10.9 Maverick, comes equipped with Python 2.7.5, which is the latest version:

Tests-Mac:~ test$ python
Python 2.7.5 (default, Aug 25 2013, 00:04:04)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

Previous versions such as Mac OS X 10.8 Mountain Lion and Mac OS X 10.7 Lion included Python 2.7.2 and Python 2.7.1 respectively, which are also compatible versions for this book. If you are an experienced Python user or someone who wants to work with the latest version of Python, you can download the latest version from http://www.python.org/getit.

Older versions of Mac OS X such as Snow Leopard and later, which came with an older version of Python, can be updated to the latest version by downloading and installing it from http://www.python.org/getit.

Installing Setuptools

Setuptools is a library containing a collection of utilities for building and distributing Python packages. The most important tool from this collection is called easy_install. It allows a user to look into PyPI, the Python package repository that we mentioned previously, and provides a simple interface to install any package by name. The easy_install utility automatically downloads, builds, installs, and manages packages for the user. This utility has been used in the later part of this book to install the necessary packages required for the upcoming projects of Python and Arduino. Although easy_install has been used as a simple way of installing Python packages, it misses out on a few useful features such as tracking actions, support for uninstallation, and support for other version control systems. In recent years, the Python community has started adopting another tool called pip over easy_install that supports these features. As both easy_install and pip utilize the same PyPI repository, going forward, you can use any of these utilities to install the required Python packages.

Just to narrow down the scope, we will be focusing on methods to install Setuptools and the default utilities that get installed with it, that is, easy_install. Later in this section, we will also install pip, just in case you want to use it too. Let's first begin by installing Setuptools for the various operating systems.

Linux

In Ubuntu, Setuptools is available in the default repository and it can be installed using the following command:

$ sudo apt-get install python-setuptools

For Fedora, it can be installed using the default software manager yum:

$ sudo yum install python-setuptools

For other Linux distributions, it can be downloaded and built using the following single-line script:

$ wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py -O - | sudo python

Once it is installed on your Linux distribution, easy_install can be directly accessed from the terminal as a built-in command.

Windows

Installation of Setuptools is not that straightforward for Windows as compared to Linux. It requires the user to download the ez_setup.py file from the Windows section at https://pypi.python.org/pypi/setuptools.

Once this is downloaded, press Shift and right-click in the folder where you downloaded the ez_setup.py file. Select Open command window here and execute the following command:

> python ez_setup.py

This will install Setuptools in the Scripts folder of your default Python installation folder. Using the same method that we used when we added Python to Environment Variables, now include Setuptools by adding C:\Python27\Scripts to PATH, followed by the semicolon (;).

This will enable the installation of various Python packages using easy_install to your Python packages folder called Libs. Once you have added the package manager to the environment variables, you need to close and reopen the command prompt for these changes to take effect.

Mac OS X

Setuptools can be installed in Mac OS X using any of the following methods. It is advisable for beginners to use the first method, as the second method requires the external package manager Homebrew.

If you have never worked with Homebrew before, you will need to follow these steps to install Setuptools on your Mac:

  1. Download ez_setup.py from the Unix/Mac section at https://pypi.python.org/pypi/setuptools.

  2. Open the terminal and navigate to the directory where you downloaded this file. For most browsers, the file gets saved to the Download folder.

  3. Run the following command in the terminal to build and set up Setuptools:

    $ sudo python ez_setup.py
    

If you are familiar with Homebrew-based software installation, just follow these quick steps to install Setuptools:

  1. First, install wget from Homebrew if you don't have it already:

    $ brew install wget
    
  2. Once you have installed wget, run the following command in the terminal:

    $ wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py -O - | python
    

    Note

    More information regarding the Homebrew utility can be obtained from http://brew.sh.

    You can install Homebrew on your Mac by running the following simple script in the terminal:

    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
    

Installing pip

As you have successfully installed Setuptools, let's use it to install pip. For Linux or Mac OS X, you can run the following command in the terminal to install pip:

$ sudo easy_install pip

For Windows, open the command prompt and execute the following command:

> easy_install.exe pip

If you have already installed pip on your computer, please make sure that you upgrade it to the latest version to overcome the few bugs that are associated with the upgrade. You can upgrade pip using the following command at the terminal:

$ sudo easy_install --upgrade pip

Since you have already used easy_install to install a Python package, let's get ourselves more familiar with Python package management.

Installing Python packages

With the installation of pip, you have two different options to install any third-party Python package listed on the PyPi repository (http://pypi.python.org). The following are the various procedures that you need to know to work with the installation of Python packages. In the following examples, the term PackageName is a pseudo name that is used for a Python package that you want to work with. For your package of choice, identify the appropriate package name from the PyPi website and put its name in place of PackageName. In some cases, you will need root (super user) privileges to install or uninstall a package. You can use sudo followed by an appropriate command for these cases.

To install a Python package, execute the following command at the terminal:

$ easy_install PackageName

Otherwise, you can also execute the following command:

$ pip install PackageName

If you want to install a specific version of a package, you can use the following command:

$ easy_install "PackageName==version"

If you are not aware of the exact version number, you can also use comparison operators such as >, <, >=, or <= to specify a range for the version number. Both easy_install and pip will select the best matching version of the package from the repository and install it:

$ easy_install "PackageName > version"

Meanwhile, for pip, you can use the following identical commands to perform similar operations:

$ pip install PackageName==version
$ pip install "PackageName>=version"

As an example, if you want to install a version between 1.0 and 3.0, you will need to use the following command:

$ pip install "PackageName>=0.1,<=0.3"

It is really easy to upgrade a package using either easy_install or pip. The command options used by both are also very similar:

$ easy_install --upgrade PackageName
$ pip install --upgrade PackageName

Although easy_install doesn't support clean uninstallation of a package, you can use the following command to make sure that Python stops searching for the specified package. Later, carefully remove the package files from the installation directory:

$ easy_install -mxN PackageName

A much better way to perform a clean uninstallation of the majority of packages is to use pip instead of easy_install:

$ pip uninstall PackageName

A detailed list of the Python packages supported by Setuptools can be found at the PyPI website at https://pypi.python.org/.