You are here: home python installation tips

Tips for installing Python on Windows

How and where to install

Here are a few tips for Windows users to install Python for scientific use.
First make sure to install a recent stable release of python, Numpy and SciPy. Do not install an experimental beta version of python, as there may be inconsistencies with Numpy and SciPy. If necessary, uninstall any previous version with the same version number x.x (for example: python 2.7.6 will be installed in a directory C:\\python27 and python 3.2.3 in a C:\\python32; these may exist simultaneously). Then install python first (www.python.org). Subsequently install Numpy from sourceforge.net and after that SciPy from sourceforge.net. Make sure that the numpy and scipy downloads are compatibel with your version of python and your computer (32 or 64 bit). For example, the file 'Numpy 1.6.2-win32-superpack-python3.2.exe' is compatible with a 32-bit Windows system on which Python 3.2 has been installed. See also the Numpy and SciPy websites. Both numpy and scipy will be installed in the directory C:\\pythonxx\Lib\site-packages and will be accessible from a python shell. It is advised to use the idle.py interface and make a shortcut to idle.exe on your desktop, if the installation has not done so already. (idle.exe can be found in C:\\pythonxx\Lib\idlelib\)

Python 2 or Python 3?

During the last years python has been in the process of slowly converting from python2 (2.6 and 2.7) to python3 (3.2.3 in July 2012). This is a major overhaul in which some compatibility with python2 is lost. It has taken some time for auxilliary packages to follow, but as both numpy and scipy are now available in python3 versions, it is highly recommended to use python3 from 2012 onward.

For most users the changes are minor. In python3 the print statement is a function with mandatory parentheses. So change "print 'some text'" into "print('some text')". Easy, and the program '2to3.py' will do it for you. The version with parentheses also works in python2! More subtle is the change in integer division: in python2 the forward slash is an integer division like 3/2=1 but in python 3 the slash is a float division like 3/2=1.5 So it is no longer necessary to write 3/2. if you want to obtain 1.5, but there is no harm in keeping such a notation, as it will still work in python3. In python3 integer division is invoked with a double forward slash (3//2=1). Be careful with such tests as n==2*(n/2) for a boolean indicating an even value. Change the slash to a double slash and you are OK in python3, but not in python2. If you want compatiblity with python 2 and 3, write n==2*int(n/2). Major changes concern the handling of text (unicode only) and bytes, but that does not concern most scientific users. A change that could concern you is that the command execfile() does not exist anymore. Executing a file 'project.py' in the python shell by using "execfile('project.py')" is no longer possible. You should now run the file by key F5 or by using the 'run module' command in the 'run' tab.

Use a working directory

Your python package will reside in a folder C:\Pythonxx\. Numpy and SciPy are to be found under C:\Pythonxx\Lib\site-packages. The IDLE user interface is C:\Pythonxx\Lib\idlelib\idle.py, which must be invoked to start an interactive Python session. It is a good idea to copy this file to a working directory, for example a directory "python" under "My Documents" and work from there. In this way the C:\Pythonxx\ directory will not be contaminated with personal files. It is then a lot easier to install a new version of python, Numpy and/or SciPy without touching your personal files. Instead of copying the idle.py file to a working directory, you can also create a shortcut to the desktop and specify the working directory in which the program should start under "properties" (right mouse click). In any case, when the file 'sitecustomize.py' (see below) is executed, the current directory changes automatically to the working directory. You can always check where you are, and what is included in the PATH variable, by typing in the Idle window:

>>> import os,sys
>>> os.getcwd()
>>> sys.path

Update sys.path

Using a personal working directory requires that this directory is included in the search path sys.path that Python uses to find modules. You may wish to create a further subdirectory "mymodules" (or whatever) under your working directory; such subdirectories should also be added to the path. There are several ways to do that, but I recommend the following.

When a Python session is started, it will automatically load the module "site.py", which tries to import a file sitecustomize.py. This file is originally not present, but the user can create it, best in the subdirectory ..\site-packages. This file should contain additions to sys.path to allow the system to reach your working directories. For example:

# this file <sitecustomize.py> must be located in a reachable directory
# e.g. in ...\site-packages
# this file is executed at the beginning of each session
# it appends user directories to sys.path and it changes the current directory
# into the defined working directory
import sys,os
sys.workdir='C:\\Documents and Settings\\me\\My Documents\\python'
if sys.workdir not in sys.path: sys.path.append(sys.workdir)
sys.mymodules=sys.workdir+'\\mymodules'
if sys.mymodules not in sys.path: sys.path.append(sys.mymodules)
os.chdir(sys.workdir)

You should of course use the correct path names for your directories. The file can be extended when needed.

Initialize your session

Before doing any work, run an initialization file to load whatever you wish to load or define for the kind of application you will run. Let us call this file start.py; it can best be localized in your working directory (which is now available). You can define several initialization files if you normally run one of several types of application. The file is seen as a module and run by typing the statement

>>> from start import *

For example, the following file will import all of Numpy and thus extend Python with numerical n-dimensional arrays and all the attributes, methods and functions that Numpy offers. It does not import Numpy's subpackages, but you may wish to use the functions rand() and randn() that generate an array of uniformly distributed, resp normally distributed random numbers, which reside in the subpackage numpy.random. The file also imports all of the plotting module plotsvg.

# this is module start.py preparing your session for numeric projects
# usage: command 'from start import *'
from numpy import *
from plotsvg import *
import sys
from numpy.random import rand,randn
print('Numpy imported: you can work with arrays')
print('rand(n) [uniform] and randn(n) [normal]: array of n random numbers')
print("plotsvg imported: see plotsvg user guide under 'Help'")
print('Your working directory is ',sys.workdir)

All so-called universal functions are defined and act on scalars as well as arrays. The following example shows that an array [0,1,2,3] can be multiplied by a complex number to yield a complex array that can be exponentiated. Functions like exp and constants like pi are already defined by Numpy.

>>> phi=0.5*pi*arange(4)
>>> i=0.+1.j
>>> z=exp(i*phi)
>>> print(z.real)
[ 1.00000000e+00 6.12303177e-17 -1.00000000e+00 -1.83690953e-16]
>>> print(z.imag)
[ 0.00000000e+00 1.00000000e+00 1.22460635e-16 -1.00000000e+00]

Note that this file does not import SciPy. When you tend to use SciPy regularly it is recommended to import all of Scipy. SciPy forms an interface with Numpy, so that the separate import of Numpy is not needed. If no further actions are required upon initialization, it is sufficient to issue the command

>>> from scipy import *

The following start file imports scipy, plotsvg and a module with physical constants in SI units physcon as pc; e.g. pc.h is Planck's constant and can be used as a constant in calculations (see here for downloading this module).

# this is module start.py preparing your session for numeric projects
# usage: command 'from start import *'
from scipy import *
from plotsvg import *
import physcon as pc
import sys
print('SciPy imported: you can work with arrays')
print("plotsvg imported: see plotsvg user guide under 'Help'")
print('Physical constants in module pc. Type pc.help()')
print('Your working directory is ',sys.workdir)

This file does not import SciPy's subpackages such as fftpack, integrate, linsolve, stats, etc., which need to be imported when needed (or added to this file). An example is a general program intersection(f1,f2,xinterval) that solves x in the interval xinterval=[x1,x2] for which f1(x)=f2(x), where f2 can be either a function or a constant. This function uses the scipy zero-search method brentq that resides in the scipy subpackage optimize:

def intersection(f1,f2,xinterval):
   from scipy import optimize
   if isinstance(f2,(int,float)):
      def fun(x): return f1(x)-f2
   else:
      def fun(x): return f1(x)-f2(x)
   return optimize.brentq(fun,xinterval[0],xinterval[1])

The execution of start.py is only a recommendation. Alternatively, such commands could also be incorporated into the file <sitecustomize.py>; however, this is not recommended as it is better to keep specific initializations visible in the working directory rather than in the python package.

Scipy documentation

Free online documentation for SciPy is available from docs.scipy.org. For your and my convenience I have added an html documentation file on arrays that gives a quick reference to the attributes, methods and functions for arrays. A similar documentation file on strings is available. Both documentation files are still in the process of improvement. So, if you copy these files for your own use, check this site regularly for improved versions. The version date is in the meta data in the file header.