From charlesreid1

No edit summary
Line 100: Line 100:


=Packages=
=Packages=
# Read about the role of built-in Pythons and various other available Pythons on the Mac: [http://www.python.org/download/mac/ http://www.python.org/download/mac/]
# Read about the various Python distributions for Mac here: [http://wiki.python.org/moin/MacPython/PythonDistributionsForMac http://wiki.python.org/moin/MacPython/PythonDistributionsForMac]
# Download the Python binary from here: [http://www.python.org/download/releases/ http://www.python.org/download/releases/]


==Py4Sci==
==Py4Sci==
{{Main|Py4Sci}}


The Py4Sci (Python 4 Science) suite consists of 4 Python extensions, which combine to provide a Matlab-like environment. These extensions are:
The Py4Sci (Python 4 Science) suite consists of 4 Python extensions, which combine to provide a Matlab-like environment. These extensions are:
Line 110: Line 117:
* [http://matplotlib.sourceforge.net/ Matplotlib] - provides 2D plotting functionality to Python
* [http://matplotlib.sourceforge.net/ Matplotlib] - provides 2D plotting functionality to Python


 
While the official installation instructions provided by the Numpy and Scipy projects are helpful, they are minimal. I've got more installation instructions at the [[Py4Sci]] page.
 
===Py4Sci on Linux===
 
This is very straightforward, given that most every distribution's package manager has the above 4 Python extensions (in fact, many distributions already have at least some of the above packages, esp. iPython). Use the following commands for Debian-based distributions (e.g. Ubuntu):
 
<pre>
$ apt-get install ipython
$ apt-get install numpy
$ apt-get install scipy
$ apt-get install matplotlib
</pre>
 
where one may replace "apt-get" with the appropriate package manager (e.g. "yum" for Fedora).
 
=== Py4Sci on Mac ===
 
This was formerly a very difficult and frustrating process, which has since become much easier and much more streamlined.
 
Instructions for installing SciPy and NumPy on Mac OS X are here: http://www.scipy.org/Installing_SciPy/Mac_OS_X
 
Installing Matplotlib is easy too: just download the source code, or your platform's binary, from the Matplotlib SourceForge page: http://sourceforge.net/projects/matplotlib/
 
Finally, you can download iPython for Mac by going to the iPython downloads page: http://ipython.scipy.org/moin/Download (installation instructions are contained in the iPython documentation, linked from that page)
 
<!--
 
Three links provide the information needed to install the above extensions for Python on Mac OS X.
 
# Read about the role of built-in Pythons and various other available Pythons on the Mac: [http://www.python.org/download/mac/ http://www.python.org/download/mac/]
# Read about the various Python distributions for Mac here: [http://wiki.python.org/moin/MacPython/PythonDistributionsForMac http://wiki.python.org/moin/MacPython/PythonDistributionsForMac]
# Download the Python binary from here: [http://www.python.org/download/releases/ http://www.python.org/download/releases/]
-->
 
 
 
===Py4Sci on Windows===
 
The instructions are similar to those for Mac...
 
SciPy and NumPy installation instructions here: http://www.scipy.org/Installing_SciPy/Windows
 
Matplotlib installation can be done using the Windows binary, available here: http://sourceforge.net/projects/matplotlib/
 
iPython installation is also easy, using the Windows binary provided here: http://ipython.scipy.org/moin/Download
 
 


==Cantera==
==Cantera==

Revision as of 17:08, 12 May 2011

Python is a handy language whose syntax is easy to learn. Python is a scripting language that is similar in syntax to Matlab, but has the power of object-oriented languages such as C++. Python is also extensible, and many libraries and packages are available for nearly every function imaginable. For example, Numpy and Scipy provide tools often used in scientific programming, such as matrix and vector objects, not inherently provided by Python.

Installing Python

Mac

Mac comes with a version of Python built-in, but this version of Python is 2.5, at least 2 major versions behind the latest. It is recommended that you install the latest version of Python 2.x (stick with 2.x, since Python 3.x makes some very major changes and probably won't work as expected).

Python versions can be obtained here: http://www.python.org/download/releases/

Pick the version you want, and when you click on it, you'll be offered downloads of the source code and binaries for several platforms.

Location of Installed Python

Mac OS X comes with a built-in version of Python, located at

/usr/bin/python

This is the wrong version of python, as it's a system version. It's best to just leave this version alone, as the system still uses it to do different things.

Install a version of Python from the above Python.org web site.

Using the Python.org version of Python will install the Python executable to the following location:

/Library/Frameworks/Python.framework

There is a "Python" binary in this folder which always points to the latest version of Python. Alternatively, you can find specific versions of Python at

/Library/Frameworks/Python.framework/Versions/2.7/python

There is also a "Current" version that will point to the most up-to-date version of Python.

Once you've installed the Python.org version of Python, you will need to add these locations to your path, in order to use this version of Python. This can be done with your Dot files (such as .profile or .bash_profile).

(Alternatively, the Python installer may automatically add this to your path by modifying .bash_profile itself).

Linux

You can use your package manager to install a binary Python, or you can download the source code from here: http://www.python.org/download/

For instructions related to building software from source, go here: Compiling Software


Windows

You can download the latest version of Python for Windows from this page: http://www.python.org/download/

It is highly recommended that you install Python in the root of your C drive: when it asks you for an install location, specify C:\Python27.

You can then add folder locations to your $PYTHONPATH variable by editing the Windows environment variables. On Windows XP, right-click on My Computer, click the "Advanced" tab, and click the "Environmental Variables" button. On Windows 7, right-click on Computer, click the "Advanced settings..." link on the left-hand side, and click the "Environmental Variables" button. Once you're there, you'll create a new user variable: variable name PYTHONPATH and variable value C:\Python27 (if you want to add other locations, separate them using a semicolon; don't delete any locations that are already there, just add locations).

Virtual Python: virtualenv

If you don't want to deal with the difficulties of installing a system version of Python, or you don't have the permissions to do so, you have another option: install a virtual version of Python, called virtualenv.

This can be obtained from here: http://pypi.python.org/pypi/virtualenv

Essentially you install it to a prefix, where it will create a /prefix/bin/ and /prefix/lib/ directory. Then a python binary is put into /prefix/bin/, and it wraps the system python and so can load all relevant libraries, etc., but can also load libraries in /prefix/lib/ so that you can extend Python, without ever touching the system version of Python.

Install it by running:

$ python setup.py install --prefix=${HOME}/pkg/virtualenv

or wherever you want to install it.

Then, whenever you want to install a Python package and make it loadable by virtualenv, you just append the same --prefix to the end of setup.py:

$ cd /path/to/package
$ python setup.py install --prefix=${HOME}/pkg/virtualenv


Python Settings

Python Path

Python uses modules, which are basically libraries of functions or code that can be downloaded and used. Some examples include Numpy and Scipy (described in further detail below, in the Py4Sci section. To import a module named foo, you would type (at the Python shell/interpreter):

>> import foo

When this is typed, Python looks in a couple of different locations for the module, contained in a file named foo.py. These locations are defined in an environmental variable called $PYTHONPATH. So basically, if you want to write your own module, or if you install a module that does not automatically update $PYTHONPATH (most modules will), you will need to modify $PYTHONPATH manually.

This variable is created with the same syntax as the system $PATH variable; that is, locations are separated by commas, and assuming the module foo.py is in /path/to/module, you can add to the existing value of $PYTHONPATH by putting this in your .profile file:

export $PYTHONPATH="${PYTHONPATH}:/path/to/module"

Packages

  1. Read about the role of built-in Pythons and various other available Pythons on the Mac: http://www.python.org/download/mac/
  2. Read about the various Python distributions for Mac here: http://wiki.python.org/moin/MacPython/PythonDistributionsForMac
  3. Download the Python binary from here: http://www.python.org/download/releases/


Py4Sci

The Py4Sci (Python 4 Science) suite consists of 4 Python extensions, which combine to provide a Matlab-like environment. These extensions are:

  • iPython - provides an enhanced Python shell
  • Numpy - an extension providing numerical routines for vector and matrix objects
  • Scipy - an extension providing MATLAB-like functionality (optimization, Fourier transforms, ODE solvers, etc.), typically used in conjunction with data types provided through the Numpy extension
  • Matplotlib - provides 2D plotting functionality to Python

While the official installation instructions provided by the Numpy and Scipy projects are helpful, they are minimal. I've got more installation instructions at the Py4Sci page.

Cantera

Cantera is a package that provides a Python interface for performing thermochecmial calculations. You can find information about installing Cantera at the Cantera page. For examples of how to use Cantera, including the Python interface, visit the Cantera Lecture page.