User Tools

Site Tools


other:python:starting

This is an old revision of the document!


Working with Python

In order to begin using Python, you need to have a Python distribution installed. A distribution provides a Python interpreter, and Python extensions (aka Python modules). You may have several distributions installed on your computer and you need to know how to initialize them, and which one you are using at a given time (type which python on Linux)

If you are using a Linux computer or a Mac, you should already have a default python installed. The following example shows where the python interpreter is installed on the asterix1 server (if it's in /usr/bin, it's the default python) and which version it is (version 2.6.6 compiled in May 2015)

jypeter@asterix1 - ...jypeter - 41 >which python
/usr/bin/python

jypeter@asterix1 - ...jypeter - 47 >rpm -qf /usr/bin/python
python-2.6.6-64.el6.x86_64

jypeter@asterix1 - ...jypeter - 42 >python
Python 2.6.6 (r266:84292, May 22 2015, 08:34:51)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-15)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

Some python distributions

This section will help you choose a distribution in the big Python ecosystem (many distributions, python version 2.7.* or 3.*, …). You should use a distribution that is already available near your data (e.g. do the computation on a server near your data, do not move/duplicate the data!).

Only install a distribution yourself if you need it on your local computer, or if you need to install some modules that can't be installed by the contacts listed below. A python distribution can be quite big, so do not install it in a backed up home directory!

We suggest that you use Python 2 rather than Python 3. Or you can check the differences between both versions and try to write scripts that will work in both versions! 8-)

LSCE distribution

Contact: the LSCE system administrators

jypeter@asterix1 - ...jypeter - 43 >module avail
[...]
castem/12        ferret/6.9.5     hdf5/1.8.9       netcdf/4p        python/2.7.5     sun-java/7.0.45
[...]

jypeter@asterix1 - ...jypeter - 44 >module load python/2.7.5

jypeter@asterix1 - ...jypeter - 45 >which python
/usr/local/install/python-2.7.5/bin/python

jypeter@asterix1 - ...jypeter - 46 >python
Python 2.7.5 (default, Sep 18 2013, 15:47:43)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

TGCC distribution

Contact: the TGCC hotline

[p25jype@curie70 ~]$ module avail
[...]
cmake/2.8.9(default)      hwloc/1.5                 netcdf-utils/4.3.3.1_hdf5 python/2.7.8              tix/8.4.3
cmake/3.2.2               hwloc/1.7.1(default)      octave/3.6.3              python/3.3.2              tk/8.5(default)
[...]

[p25jype@curie70 ~]$ module load python/2.7.8
load module python/2.7.8 (Python)

[p25jype@curie70 ~]$ which python
/usr/local/ccc_python/2.7.8_201409/bin/python

[p25jype@curie70 ~]$ python
Python 2.7.8 (default, Aug 27 2014, 17:50:16)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

UV-CDAT

Contact: Jean-Yves @ LSCE

What is UV-CDAT?

UV-CDAT (Ultrascale Visualization - Climate Data Analysis Tools) is a python distribution developed specifically for the climate scientists.

You can find out which standard packages are installed by clicking on the cdat-VVV_JYP-NNN_build_info.txt files below and by visiting the Extra packages installed in UV-CDAT page.

UV-CDAT also provides some specific packages: cdms2, cdutil, cdtime, genutil, vcs, cmor2, …

UV-CDAT versions maintained by JYP

The following versions are maintained by JYP on the servers where LSCE users have accounts

CDAT version JYP version python version Packages built by CDAT Installation notes
1.1.0 08 2.7.3 n/a 1.1.0 notes
1.5.1 10 2.7.4 cdat-1.5.1_jyp-10_build_info.txt 1.5.1 notes
2.1.0 13 2.7.8 cdat-2.1.0_jyp-13_build_info.txt 2.1.0 notes
2.4.0 14 Not installed yet

Notes:

  • The cdat-VVV_jyp-NNN_build_info.txt files have been generated with
    cd /my_build_directory_path; cat build_info.txt | sort > cdat-VVV_JYP-NNN_build_info.txt

Initializing UV-CDAT

The way you initialize UV-CDAT depends on which server, which version and which shell you are using!

Canopy

Enthought Canopy is a Scientific and Analytic Python Deployment with Integrated Analysis Environment provided by Enthought. It is available for Windows, Mac and Linux. You can download the free Canopy Express that will already provide many extensions. If you are entitled to use Canopy Academic, login from inside Canopy Express and download the extra modules you need

Anaconda

Anaconda is a distribution similar to canopy provided by Continuum Analytics. It is available for Windows, Mac and Linux

Launching Python

Once you have initialized the python distribution you want to use, follow the instructions below to start the interpreter from a shell (on a Windows computer, you will have to start python from the Start menu).

Remember that you can check which python you are using by typing which python!

Useful keyboard shortcuts

Key Effect
CTRL-D Exit the interpreter
↑ and ↓ Go to previous/next line(s)
CTRL-A Go to the beginning of the line
CTRL-E Go to the end of the line
CTRL-K Erase from the cursor to the end of the line
CTRL-U Erase from the beginning of the line to the cursor
TAB x 2 Do some TAB-completion (context dependent)
e.g. a = np.cumTABTAB
CTRL-C Interrupt a running script
CTRL-Z Suspend the interpreter and go back to the shell
Do not forget to go back to the interpreter with fg
or to kill it (with jobs and kill %NN)

Stand-alone script

A python script is just like any other shell script. You don't have to explicitly call the python interpreter, if the interpreter is specified in a #! shebang comment on the first line of the script with, and the script has it's execution bit set.

jypeter@asterix1 - ...jypeter - 54 >cat basic_script.py
#!/usr/bin/env python

import sys

script_name = sys.argv[0]

print('Hello world, I am the ' + script_name + ' script')

# The end

jypeter@asterix1 - ...jypeter - 55 >chmod +x basic_script.py

ypeter@asterix1 - ...jypeter - 56 ># Type here what is required to initialize the distribution you want

jypeter@asterix1 - ...jypeter - 57 >./basic_script.py
Hello world, I am the ./basic_script.py script

Standard interpreter

  • python: start the interpreter
  • python script.py: execute script.py and exit
  • python -i script: execute script.py and stay in the interpreter

Type man python if you want to see what other command line options are available

ipython interpreter

The ipython interpreter provides more options and commands than the standard python interpreter, but takes more time to load. If you are going to develop by starting and exiting the interpreter many times, it's faster to use the standard interpreter

Starting ipython: ipython

ipython notebook

The ipython notebook is a way to interact with python (and other supported interpreted languages) inside a web browser. You can mix cells with python commands, cells with the output of the python commands (possibly graphics generated by the commands), and text (using some wiki-like rich text format). This interactive web page, aka notebook, can be saved in a my_notebook.ipynb file and re-used later (e.g. the notebook provided in the introduction to Python, part 1).

Starting the notebook server: ipython notebook

Warning! When you start the notebook server, you will start a python process and a web browser, and a new python process will be started each time you open a new notebook.

Please make sure that you shutdown cleanly each notebook and the server when you are finished, in order not to clutter the local computer or remote server with lots of python processes (especially a server shared by many users!).

You may want to use the 'top' command in order to monitor what is happening





[ PMIP3 Wiki Home ] - [ Help! ] - [ Wiki syntax ]

other/python/starting.1454497019.txt.gz · Last modified: 2016/02/03 10:56 by jypeter