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. Uninstall any previous versions; then install python first (www.python.org). Subsequently install Numpy and then SciPy from www.scipy.org.

Use a working directory

Your python package will reside in a folder C:\Python25\ (or something like this). Numpy and SciPy are to be found under C:\Python25\Lib\site-packages. The IDLE user interface is C:\Python25\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:\Python25\ folder 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. In stead 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 init.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 run by typing the statement

>>> execfile("init.py")

For example, the following file will import all of SciPy and thus extend Python with numerical arrays and the full range of numerical applications that SciPy offers. It 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). This file also imports a user module which is not part of the python distribution:
physcon containing all relevant physical constants in SI units; e.g. pc.h is Planck's constant and can be used as a constant in calculations
See here for downloading this module.
Init.py will also set the current directory to your working directory.

# this file <init.py> is to be executed at the beginning of each Python session
from scipy import *
import physcon as pc
print 'scipy loaded'
print 'physical constants in module pc. Type pc.help()'

The user should of course tailor this file for his needs. The standard import of SciPy is highly recommended: SciPy forms an interface with Numpy, so that the separate import of Numpy is not needed. 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 SciPy.

>>> 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]

The execution of init.py is only a recommendation. If no further actions are required upon initialization, it is sufficient to issue the command

>>> from scipy import *

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.

As noted above, the subpackages of SciPy have not yet been imported. So, when you need routines from the linear algebra subpackage linalg, this is made available by the command

>>>from scipy import linalg

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.