What you need for this book
This book is aimed at Python 3. While many of the examples will work in Python 2, you'll get the best experience working through this book with a recent version of Python 3. At the time of writing, the latest version is 3.4.3, and the examples were tested against this.
Though Python 3.4 is the preferred version, all the examples should run on Python 3.1 or later, except for the following:
- The
asyncio
example in Chapter 8, Client and Server Applications, as theasyncio
module was only included in Version 3.4 - The Flask example in Chapter 9, Applications for the Web, which requires Python 3.3 or later
We're also targeting the Linux operating system, and the assumption is made that you are working on a Linux OS. The examples have been tested on Windows though, and we'll make a note of where there may be differences in the requirements or outcomes.
Virtual environments
It is highly recommended that you use Python virtual environments, or "venvs", when you work with this book, and in fact, when doing any work with Python. A venv is an isolated copy of the Python executable and associated files, which provides a separate environment for installing Python modules, independent from the system Python installation. You can have as many venvs as you need, which means that you can have multiple module configurations set up, and you can switch between them easily.
From version 3.3, Python includes a venv
module, which provides this functionality. The documentation and examples are available at https://docs.python.org/3/using/scripts.html. There is also a standalone tool available for earlier versions, which can be found at https://virtualenv.pypa.io/en/latest/.
Installing Python 3
Most major Linux distributions come preinstalled with Python 2. When installing Python 3 on such a system, it is important to note that we're not replacing the installation of Python 2. Many distributions use Python 2 for core system operations, and these will be tuned for the major version of the system Python. Replacing the system Python can have severe consequences for the running of the OS. Instead, when we install Python 3, it is installed side by side with Python 2. After installing Python 3, it is invoked using the python3.x
executable, where x
is replaced with the corresponding installed minor version. Most packages also provide a symlink
to this executable called python3
, which can be run instead.
Packages to install Python 3.4 are available for most recent distributions, we'll go through the major ones here. If packages are not available, there are still some options that you can use to install a working Python 3.4 environment.
Ubuntu and Debian
Ubuntu 15.04 and 14.04 come with Python 3.4 already installed; so if you're running these versions, you're already good to go. Note that there is a bug in 14.04, which means pip must be installed manually in any venvs created using the bundled venv
module. You can find information on working around this at http://askubuntu.com/questions/488529/pyvenv-3-4-error-returned-non-zero-exit-status-1.
For earlier versions of Ubuntu, Felix Krull maintains a repository of up-to-date Python installations for Ubuntu. The complete details can be found at https://launchpad.net/~fkrull/+archive/ubuntu/deadsnakes.
On Debian, Jessie has a Python 3.4 package (python3.4
), which can be installed directly with apt-get
. Wheezy has a package for 3.2 (python3.2
), and Squeeze has python3.1
, which can be installed similarly. In order to get working Python 3.4 installations on these latter two, it's easiest to use Felix Krull's repositories for Ubuntu.
RHEL, CentOS, Scientific Linux
These distributions don't provide up-to-date Python 3 packages, so we need to use a third-party repository. For Red Hat Enterprise Linux, CentOS, and Scientific Linux, Python 3 can be obtained from the community supported Software Collections (SCL) repository. Instructions on using this repository can be found at https://www.softwarecollections.org/en/scls/rhscl/python33/. At the time of writing, Python 3.3 is the latest available version.
Python 3.4 is available from another repository, the IUS Community repository, sponsored by Rackspace. Instructions on the installation can be found at https://iuscommunity.org/pages/IUSClientUsageGuide.html.
Fedora
Fedora 21 and 22 provide Python 3.4 with the python3
package:
$ yum install python3
For earlier versions of Fedora, use the repositories listed in the preceding Red Hat section.
Alternative installation methods
If you're working on a system, which isn't one of the systems mentioned earlier, and you can't find packages for your system to install an up-to-date Python 3, there are still other ways of getting it installed. We'll discuss two methods, Pythonz
and JuJu
.
Pythonz
Pythonz is a program that manages the compilation of Python interpreters from source code. It downloads and compiles Python from source and installs the compiled Python interpreters in your home directory. These binaries can then be used to create venvs. The only limitation with this installation method is that you need a build environment (that is, a C compiler and supporting packages) installed on your system, and dependencies to compile Python. If this doesn't come with your distribution, you will need root access to install this initially. The complete instructions can be found at https://github.com/saghul/pythonz.
JuJu
JuJu can be used as a last resort, it allows a working Python 3.4 installation on any system without needing root access. It works by creating a tiny Arch Linux installation in a folder, in your home folder and provides tools that allow us to switch to this installation and run commands in it. Using this, we can install Arch's Python 3.4 package, and you can run Python programs using this. The Arch environment even shares your home folder with your system, so sharing files between environments is easy. The JuJu home page is available at https://github.com/fsquillace/juju.
JuJu should work on any distribution. To install it we need to do this:
$ mkdir ~/.juju $ curl https:// bitbucket.org/fsquillace/juju-repo/raw/master/juju- x86_64.tar.gz | tar -xz -C ~/.juju
This downloads and extracts the JuJu image to ~/.juju
. You'll need to replace the x86_64
with x86
if you're running on a 32-bit system. Next, set up PATH
to pick up the JuJu commands:
$ export PATH=~/.juju/opt/juju/bin:$PATH
It's a good idea to add this to your .bashrc
, so you don't need to run it every time you log in. Next, we install Python in the JuJu
environment, we only need to do this once:
$ juju -f $ pacman --sync refresh $ pacman --sync --sysupgrade $ pacman --sync python3 $ exit
These commands first activate the JuJu
environment as root, then use the pacman
Arch Linux package manager to update the system and install Python 3.4. The final exit
command exits the JuJu
environment. Finally, we can access the JuJu
environment as a regular user:
$ juju
We can then start using the installed Python 3:
$ python3 Python 3.4.3 (default, Apr 28 2015, 19:59:08) [GCC 4.7.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
Windows
Compared to some of the older Linux distributions, installing Python 3.4 on Windows is relatively easy; just download the Python 3.4 installer from http://www.python.org and run it. The only hitch is that it requires administrator privileges to do so, so if you're on a locked down machine, things are trickier. The best solution at the moment is WinPython, which is available at http://winpython.github.io.
Other requirements
We assume that you have a working Internet connection. Several chapters use Internet resources extensively, and there is no real way to emulate these offline. Having a second computer is also useful to explore some networking concepts, and for trying out network applications across a real network.
We also use the Wireshark packet sniffer in several chapters. This will require a machine where you have root access (or administrator access in Windows). Wireshark installers and installation instructions are available at https://www.wireshark.org. An introduction to using Wireshark can be found in the Appendix, Working with Wireshark.