other:python:starting
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
other:python:starting [2019/04/26 09:35] – Added Ultra quick-start on the LSCE servers jypeter | other:python:starting [2019/06/17 09:15] – Improved the intro jypeter | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Working with Python ====== | ====== Working with Python ====== | ||
- | <note tip>This page will tell you how to select a python distribution and start (and exit!) the python interpreter | + | <note tip>This page will tell you how to select a //python distribution// and start (and exit!) the python interpreter |
- | You can then read the [[jyp_steps|JYP' | + | After reading this page, you can read the [[jyp_steps|JYP' |
</ | </ | ||
Line 12: | Line 12: | ||
In order to start working with Python, you need to have a //Python distribution// | In order to start working with Python, you need to have a //Python distribution// | ||
- | 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 //obelix// LSCE servers (if it's in ''/ | + | 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 //obelix// LSCE servers (if it's in ''/ |
< | < | ||
Line 34: | Line 34: | ||
==== Initialization ==== | ==== Initialization ==== | ||
- | - Start a terminal on an //obelix// server | + | - Start a terminal on an //obelix// server |
- | - Type '' | + | - Type '' |
- | - Determine if you are using a [[other: | + | - ...or **follow the steps below to use the more complete // |
- | | + | - Determine if you are using a [[other: |
- | | + | |
+ | | ||
+ | - Choose which //flavor// of Python you want by typing the activation command: | ||
+ | * Python 2.7.x: '' | ||
+ | * Python 3.x: '' | ||
+ | - Type '' | ||
+ | - Note: if you will have to use python regularly, you should add the '' | ||
+ | * **Do not** add the '' | ||
+ | |||
+ | === Example === | ||
+ | |||
+ | < | ||
+ | Last login: Mon Jun 3 08:49:53 2019 from somewhere | ||
+ | bash-4.2$ which python | ||
+ | / | ||
+ | bash-4.2$ source ~jypeter/ | ||
+ | bash-4.2$ which python | ||
+ | / | ||
+ | bash-4.2$ conda activate cdatm_py2 | ||
+ | (cdatm_py2) bash-4.2$ which python | ||
+ | / | ||
+ | (cdatm_py2) bash-4.2$ python | ||
+ | Python 2.7.15 | packaged by conda-forge | (default, Feb 28 2019, 04:00:11) | ||
+ | [GCC 7.3.0] on linux2 | ||
+ | Type " | ||
+ | >>> | ||
+ | hello! | ||
+ | >>> | ||
+ | </ | ||
+ | |||
+ | ==== Useful keyboard shortcuts ==== | ||
+ | |||
+ | ^ Key ^ Effect | ||
+ | | **CTRL-D** | ||
+ | | **↑ and ↓** | Go to previous/ | ||
+ | | **CTRL-A** | ||
+ | | **CTRL-E** | ||
+ | | **CTRL-K** | ||
+ | | **CTRL-U** | ||
+ | | **TAB** x 2 | Do some TAB-completion (context dependent)\\ e.g. '' | ||
+ | | **CTRL-C** | ||
+ | | **CTRL-Z** | ||
+ | |||
+ | ==== Executing a python script ==== | ||
+ | |||
+ | Note: Python is an [[https:// | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | Type '' | ||
+ | |||
+ | ==== Stand-alone script ==== | ||
+ | |||
+ | You don't have to explicitly call the python interpreter, | ||
+ | |||
+ | If you have a '' | ||
+ | < | ||
+ | # | ||
+ | |||
+ | import sys | ||
+ | |||
+ | script_name = sys.argv[0] | ||
+ | |||
+ | print(' | ||
+ | |||
+ | # The end</ | ||
+ | |||
+ | ...and if you are in the same directory and have initialized the Python distribution you want, you can then run the script the following way: | ||
+ | |||
+ | < | ||
+ | |||
+ | > ls -l basic_script.py | ||
+ | -rwxr-xr-x [...] basic_script.py | ||
+ | |||
+ | > ./ | ||
+ | Hello world, I am the ./ | ||
+ | </ | ||
+ | |||
+ | ==== What next? ==== | ||
+ | |||
+ | - Read the rest of this page to get a better understanding of what you have learned in this section, and use Python more efficiently | ||
+ | - Learn Python by reading what you need on the [[other: | ||
===== Python distributions available for LSCE users ===== | ===== Python distributions available for LSCE users ===== | ||
Line 44: | Line 127: | ||
This section will help you choose a distribution in the big Python ecosystem (many distributions, | This section will help you choose a distribution in the big Python ecosystem (many distributions, | ||
- | Only install a distribution yourself if you need it on your local computer (desktop or laptop), or if you need to install some modules that can't be installed by the contacts listed below. A python distribution will require several Gb of disk space, so do not install it in a backed up //home// directory! | + | Only install a distribution yourself if you need it on your local computer (desktop or laptop), or if you need to install some modules that can't be installed by the contacts listed below. A python distribution will require several Gb of disk space, so do not install it in your backed up //home// directory! |
You can use either //Python 2// or //Python 3//. Most packages are now available in both versions, but you should make sure that the most important package/s you need is/are available in the selected Python version. You can check the [[https:// | You can use either //Python 2// or //Python 3//. Most packages are now available in both versions, but you should make sure that the most important package/s you need is/are available in the selected Python version. You can check the [[https:// | ||
Line 51: | Line 134: | ||
* Contact: the LSCE system administrators ([[help-lsce@lsce.ipsl.fr]]) | * Contact: the LSCE system administrators ([[help-lsce@lsce.ipsl.fr]]) | ||
- | * Where: //obelix// interactive servers and cluster at LSCE | + | * Where: //obelix// interactive servers and // |
* Initialization type: [[other: | * Initialization type: [[other: | ||
* What's installed: type '' | * What's installed: type '' | ||
Line 72: | Line 155: | ||
Python 2.7.15 |Anaconda, Inc.| (default, Oct 10 2018, 21:32:13) | Python 2.7.15 |Anaconda, Inc.| (default, Oct 10 2018, 21:32:13) | ||
[GCC 7.3.0] on linux2 | [GCC 7.3.0] on linux2 | ||
- | Type " | ||
- | >>> | ||
- | </ | ||
- | |||
- | ==== TGCC distribution ==== | ||
- | |||
- | * Contact: the TGCC hotline ([[hotline.tgcc@cea.fr]]) | ||
- | * Where: CEA TGCC | ||
- | * Initialization type: [[other: | ||
- | * What's installed: it depends... | ||
- | |||
- | < | ||
- | $ module avail -t -d | egrep ' | ||
- | flavor/ | ||
- | flavor/ | ||
- | flavor/ | ||
- | flavor/ | ||
- | intelpython2/ | ||
- | intelpython3/ | ||
- | python/ | ||
- | python3/ | ||
- | cdat/ | ||
- | |||
- | $ module load python | ||
- | [...] | ||
- | load module python/ | ||
- | |||
- | $ which python | ||
- | / | ||
- | |||
- | $ python | ||
- | Python 2.7.14 (default, Jan 11 2018, 16:43:59) | ||
- | [GCC 4.8.5] on linux2 | ||
Type " | Type " | ||
>>> | >>> | ||
Line 126: | Line 176: | ||
^ CDAT version | ^ CDAT version | ||
| **8.1** | | **8.1** | ||
+ | |||
+ | == Initialization == | ||
+ | |||
+ | Read the [[https:// | ||
+ | |||
+ | Note: on the ciclad cluster, use '' | ||
=== CDAT at TGCC === | === CDAT at TGCC === | ||
+ | |||
+ | Note: TGCC also supports its own [[# | ||
CDAT **8.0** is installed at TGCC and can be initialized with: | CDAT **8.0** is installed at TGCC and can be initialized with: | ||
Line 133: | Line 191: | ||
* Python 3: '' | * Python 3: '' | ||
- | === The basics of CDAT initialization | + | === A common |
- | + | ||
- | The way you initialize CDAT depends on: | + | |
- | * which **server** you want to use it on | + | |
- | * which **shell** you are using in your terminals: //tcsh// or other shells | + | |
- | * Read [[other: | + | |
- | * Warning: the shell in a (batch) script may be different from your interactive shell! | + | |
- | * which **version** of CDAT you want to use... | + | |
Note: if you get an error when importing //cdms2// or //vcs// it means that either you have forgotten to initialize CDAT, or that something went wrong during the initialization. In both cases, you are either still using the default python installed on your system, or another (non-CDAT) python distribution! | Note: if you get an error when importing //cdms2// or //vcs// it means that either you have forgotten to initialize CDAT, or that something went wrong during the initialization. In both cases, you are either still using the default python installed on your system, or another (non-CDAT) python distribution! | ||
Line 153: | Line 204: | ||
/ | / | ||
- | === conda-based versions of CDAT === | + | ==== TGCC distribution ==== |
- | <WRAP center round important 60%> | + | * Contact: the TGCC hotline ([[hotline.tgcc@cea.fr]]) |
- | \\ The steps detailed in this section are for [[other:uvcdat: | + | * Where: CEA TGCC |
- | </ | + | * Initialization type: [[other:newppl:starting# |
+ | * What's installed: it depends... | ||
- | Note: if you need to maintain a **local** | + | < |
+ | $ module avail -t -d | egrep ' | ||
+ | flavor/ | ||
+ | flavor/ | ||
+ | flavor/ | ||
+ | flavor/ | ||
+ | intelpython2/ | ||
+ | intelpython3/ | ||
+ | python/ | ||
+ | python3/ | ||
+ | cdat/8.0(default) | ||
- | == Configuring .bashrc == | + | $ module load python |
- | + | [...] | |
- | You should **add** the following line at the end of your '' | + | load module |
- | + | ||
- | ^ Server | + | |
- | | **LSCE** | + | |
- | | **ciclad** | + | |
- | + | ||
- | + | ||
- | Note: when you add the specified line to your '' | + | |
- | + | ||
- | == Initializing CDAT == | + | |
- | + | ||
- | <WRAP center round tip 60%> | + | |
- | When you initialize CDAT, it will be available only in the **current** window/ | + | |
- | </ | + | |
- | + | ||
- | - If [[other: | + | |
- | + | ||
- | * You can later go back to your previous shell by typing '' | + | |
- | - Activate the //conda environment// | + | |
- | * '' | + | |
- | * Use the table below to determine which environments are available on which server | + | |
- | - Use CDAT ! | + | |
- | + | ||
- | ^ UV-CDAT version | + | |
- | | [[: | + | |
- | | [[: | + | |
- | | **cdatm** will always point to the latest stable version | + | |
- | | //latest stable version// | + | |
+ | $ which python | ||
+ | / | ||
+ | $ python | ||
+ | Python 2.7.14 (default, Jan 11 2018, 16:43:59) | ||
+ | [GCC 4.8.5] on linux2 | ||
+ | Type " | ||
+ | >>> | ||
+ | </ | ||
==== Canopy ==== | ==== Canopy ==== | ||
Line 207: | Line 250: | ||
Anaconda is **available for Windows, Mac and Linux** | Anaconda is **available for Windows, Mac and Linux** | ||
- | Note: Anaconda provides and uses '' | + | Note: Anaconda provides and uses '' |
- | ===== Launching Python | + | ===== ipython |
- | + | ||
- | Once you have initialized the [[# | + | |
- | + | ||
- | Remember that on Linux/Mac you can check which python you are using by typing '' | + | |
- | + | ||
- | ==== Useful keyboard shortcuts ==== | + | |
- | + | ||
- | ^ Key ^ Effect | + | |
- | | **CTRL-D** | + | |
- | | **↑ and ↓** | Go to previous/ | + | |
- | | **CTRL-A** | + | |
- | | **CTRL-E** | + | |
- | | **CTRL-K** | + | |
- | | **CTRL-U** | + | |
- | | **TAB** x 2 | Do some TAB-completion (context dependent)\\ e.g. '' | + | |
- | | **CTRL-C** | + | |
- | | **CTRL-Z** | + | |
- | + | ||
- | ==== Stand-alone script ==== | + | |
- | + | ||
- | A python script is just like any other shell script. You don't have to explicitly call the python interpreter, | + | |
- | + | ||
- | < | + | |
- | jypeter@asterix1 - ...jypeter - 54 >cat basic_script.py | + | |
- | # | + | |
- | + | ||
- | import sys | + | |
- | + | ||
- | script_name = sys.argv[0] | + | |
- | + | ||
- | print(' | + | |
- | + | ||
- | # 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 > | + | |
- | Hello world, I am the ./ | + | |
- | </ | + | |
- | + | ||
- | ==== Standard interpreter ==== | + | |
- | + | ||
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | + | ||
- | Type '' | + | |
==== ipython interpreter ==== | ==== ipython interpreter ==== | ||
- | The //ipython// interpreter provides more options and commands than the standard python interpreter, | + | The //ipython// interpreter provides more options and commands than the standard python interpreter, |
Starting ipython: '' | Starting ipython: '' | ||
Line 270: | Line 264: | ||
==== ipython notebook ==== | ==== ipython notebook ==== | ||
+ | |||
+ | <note warning> | ||
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 '' | 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 '' |
other/python/starting.txt · Last modified: 2025/07/03 16:18 by jypeter