Book Image

Learning SciPy for Numerical and Scientific Computing Second Edition - Second Edition

Book Image

Learning SciPy for Numerical and Scientific Computing Second Edition - Second Edition

Overview of this book

This book targets programmers and scientists who have basic Python knowledge and who are keen to perform scientific and numerical computations with SciPy.
Table of Contents (10 chapters)
9
Index

Installing SciPy

At the time of this book, the stable production releases of Python were 2.7.9 and 3.4.2. Still, Python 2.7 is more convenient if the user needs to communicate with third-party applications. No new releases are planned for Python 2; Python 3 is considered the present and the future of Python. For the purposes of SciPy applications, we do recommend you hold on to the 2.7 version, as there are still some packages using SciPy that have not been ported to Python 3 yet. Nevertheless, the companion software of this book was tested to work on both Python 2.7 and Python 3.4.

The Python software package can be downloaded from the official site (https://www.python.org/downloads/) and can be installed on all major systems such as Windows, Mac OS X, Linux, and Unix. It has also been ported to other platforms, including Palm OS, iOS, PlayStation, PSP, Psion, and so on.

The following screenshot shows two popular options for coding in Python on an iPad—PythonMath and Sage Math. While the first application allows only the use of simple math libraries, the second permits the user to load and use both NumPy and SciPy remotely.

Installing SciPy

PythonMath and Sage Math bring Python coding to iOS devices. Sage Math allows importing NumPy and SciPy.

We shall not go into detail about the installation of Python on your system, since we already assume familiarity with this language. In case of doubt, we advise browsing the excellent book Expert Python Programming, Tarek Ziadé, Packt Publishing, where detailed explanations are given for installing many of the different implementations on different systems. It is usually a good idea to follow the directions given on the official Python website. We will also assume familiarity with carrying out interactive sessions in Python, as well as writing standalone scripts.

The latest libraries for both NumPy and SciPy can be downloaded from the official SciPy site (http://scipy.org/). They both require a Python Version 2.4 or newer, so we should be in good shape at this point. We may choose to download the package from SourceForge (http://sourceforge.net/projects/scipy/), Gohlke (http://www.lfd.uci.edu/~gohlke/pythonlibs/) or Git repositories (for instance, the superpack from http://stronginference.com/ScipySuperpack/).

It is also possible in some systems to use prepackaged executable bundles that simplify the process, such as the Anaconda (https://store.continuum.io/cshop/anaconda/) or the Enthought (https://www.enthought.com/products/epd/) Python distributions. Here, we will show you how to download and install Scipy on various platforms in the most common cases.

Installing SciPy on Mac OS X

While installing SciPy on Mac OS X, you must consider some criteria before you install it on your system. This helps in smooth installation of SciPy. The following are the things to be taken care of:

  • For instance, in Mac OS X, if MacPorts is installed, the process could not be easier. Open a terminal as superuser, and at the prompt (%), issue the following command:
    % port search scipy
    
  • This presents a list of all ports that either install SciPy or use SciPy as a requirement. For Python 2.7 we need to install py27-scipy issuing the following command:
    % port install py27-scipy
    

A few minutes later, the libraries are properly installed and ready to use. Note how macports also installs all needed requirements for us (including the NumPy libraries) without any extra effort on our part.

Installing SciPy on Unix/Linux

Under any other Unix/Linux system, if either no ports are available or if the user prefers to install from the packages downloaded from either SourceForge or Git, it is enough to perform the following steps:

  1. Unzip the NumPy and SciPy packages following the recommendation of the official pages. This creates two folders, one for each library.

    Within a terminal session, change directories to the folder where the NumPy libraries are stored, which contains the setup.py file. Find out which Fortran compiler you are using (one of gnu, gnu95, or fcompiler), and at prompt, issue the following command:

    % python setup.py build –fcompiler=<compiler>
    
  2. Once built, and on the same folder, issue the installation command. This should be all:
    % python setup.py install
    

Installing SciPy on Windows

You can install Scipy on Windows in many ways. The following are some recommended ways that you might want to have a look on:

  • Under Microsoft Windows, we recommend you install from the binary installers provided by the Anaconda or Enthought Python Distributions. Please, however, be aware of the memory requirements. Alternatively, you can download and install the SciPy stack or the libraries, individually.
  • The procedure for the installation of the SciPy libraries is exactly the same, that is, downloading and building before installing under Unix/Linux or downloading and running under Microsoft Windows. Note that different implementations of Python might have different requirements before installing NumPy and SciPy.

Testing the SciPy installation

As you might know, computer systems are not infallible. Accordingly, before starting computing via SciPy, one needs to be sure it is working correctly. To that end, SciPy developers have included a test suit any user of SciPy can execute to be sure the SciPy being used is working fine. That way, much debugging time can be saved whenever an error occurs while using any function provided by SciPy.

To run the test suite, at the Python prompt, one can run the following commands:

>>> import scipy
>>> scipy.test()

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. 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.

The reader should be aware that the execution of this test will take some time to finish. It should end with something like this:

Testing the SciPy installation

This means that at the basic level, your SciPy installation is fine. Eventually, the test could end in the form:

Testing the SciPy installation

In this case, one needs to revise carefully the errors and the failed tests. A place to get help is the SciPy mailing list (http://mail.scipy.org/pipermail/scipy-user/) to which one could subscribe. We have included a Python script that the reader could use to run these tests that can be found at the companion software for this chapter that comes with the book.