10. Installing Python

For scientific programming with Python, you need to install Python and three scientific Python libraries: NumPy, SciPy, and MatPlotLib. There are many more libraries you can install, but Python along with NumPy, SciPy, and MatPlotLib are those that are essential for scientific programming.

10.1. Installing Python

There are a number of ways to install Python and the scientific libraries you will need on your computer. Some are easier than others. You can install Python and the scientific libraries you need from “source” and compile them yourself. This is not recommended unless you are an expert in Python, in which case you have little need for this manual.

For most people, the simplest way to install Python and all the scientific libraries you need is to install either Canopy or Spyder. Canopy and Spyder are integrated development environments (IDEs) for Python. They have a number of very useful features and tools. First, they have syntax highlighting, which colors different parts Python syntax according to function, making code easier to read. Second, and more importantly, they run a program in the background called PyFlakes that checks the validity of the Python syntax as you write it. It’s like a spelling and grammar checker all rolled into one, and it is extremely useful, for novice and expert alike. The Canopy and Spyder IDEs have a number of other useful features, which we do not go into here, but expect you will learn about as you become more familiar with Python. Canopy is a simpler IDE than Spyder, and easier for novices to learn and maintain. Spyder has more advanced features, which you may find useful as you become more expert in Python programming.

Canopy is written, maintained, and distributed by the software company Enthought (http://www.enthought.com/). There are two versions of Canopy. One version, Canopy Express, is completely free to everybody and contains all the libraries you will need for this manual. The other, Canopy Basic, contains nearly every library you are ever likely to need for scientific computing. It is free to academic users; others pay a fee. Go to https://www.enthought.com/products/canopy/ and press the “Get Canopy” button, which will take you to a page where you can either download Canopy Express or request and academic license, which will allow you to download Canopy Basic.

Spyder provides a completely open source programming environment for Python. The entire Spyder distribution is free to all and can be found at https://code.google.com/p/spyderlib/. It also includes nearly all the scientific libraries you are likely to need for scientific computing.

In this manual, we assume you are using Canopy, but the Spyder interface is very similar to Canopy so that most users should have no difficulty using Spyder with this manual. If you choose to use Spyder, launch Spyder and then go to the Preferences menu and then under the Console menu, select the Advanced settings tab; tick the box Start an IPython kernel at startup (it may already be selected, in which case you need to do nothing). You only need to do this once, which sets up the IPython console when Spyder is launched. Once that is done, you should be able to follow everything written in this manual.

10.2. Testing your installation of Python

Running the Python program below tests your installation of Python to verify that the installation was successful. In particular, it tests that the NumPy, SciPy, and MatPlotLib libraries that are needed for this manual are properly installed.

If you are a student, you should input your first and last names inside the single quotes on lines 15 and 16, respectively.

Instructors can modify lines 21-23 to suit the needs of the course.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# This code tests that your Python installation worked.
# It generates a png image file that you should e-mail
# to the address shown on the plot
import scipy
import numpy
import matplotlib
import matplotlib.pyplot as plt
import platform
import socket

# If you are a student, please fill in your first and last
# names inside the quotes in the two lines below.  Do not
# modify anything else in this file

your_first_name = 'Dana'
your_last_name = 'Martin'

# If you are an instructor, modify the next 3 lines.
# You do not need to modify anything else in this file.

classname = 'Intro Phys I'
term = 'Fall_2014'      # must contain no spaces
email = 'hmwkemail@univX.edu'

plt.plot([0,1], 'r', [1,0], 'b')
plt.text( 0.5, 0.8, '{0:s} {1:s}'
        .format(your_first_name, your_last_name),
        horizontalalignment='center',
        size = 'x-large',
        bbox=dict(facecolor='purple', alpha=0.4))
plt.text( 0.5, 0.1,
    '{1:s}\nscipy {2:s}\nnumpy {3:s}\nmatplotlib {4:s}\non {5:s}\n{6:s}'
        .format(
        classname,
        term,
        scipy.__version__,
        numpy.__version__,
        matplotlib.__version__,
        platform.platform(),
        socket.gethostname()
        ) ,
    horizontalalignment='center'
    )
filename = your_last_name + '_' + your_first_name + '_' + term + '.png'
plt.title('*** E-mail the saved version of this plot, ***\n' +
    '"{0:s}" to {1:s}'.format(filename, email), fontsize=12)
plt.savefig(filename)
plt.show()