User Tools

Site Tools


other:python:starting

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
other:python:starting [2016/01/21 14:52]
jypeter More changes
other:python:starting [2019/04/26 12:20]
jypeter [Initialization]
Line 1: Line 1:
 ====== Working with Python ====== ====== Working with Python ======
  
-In order to start using Python, ​you need to have //​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 (use ''​which ​python''​ on Linux)+<note tip>This page will tell you how to select ​python ​distribution and start (and exit!the python ​interpreter
  
-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)+You can then read the [[jyp_steps|JYP'​s ​recommended steps for learning ​python]] for really working with python 
 +</note>
  
-<​code>​jypeter@asterix1 - ...jypeter - 41 >which python+Note: the former and **outdated** version (before the massive usage of //conda//) is still [[other:​python:​starting_170810|available for reference]] 
 + 
 +===== Where to start ===== 
 + 
 +In order to start working with Python, you need to have a //Python distribution//​ installed on your local computer or on the remote Linux server(s) you work on. A //​distribution//​ provides a //Python interpreter//,​ and Python extensions (aka Python //modules// or //​packages//​). 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 to determine where the python executable is located) 
 + 
 +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 ''/​usr/​bin'',​ it's the default python) and which version it is (example below: version //2.7.5// compiled in April 2019) 
 + 
 +<​code>​# Which is the current python (e.gwhere is it located)? 
 + > which python
 /​usr/​bin/​python /​usr/​bin/​python
  
-jypeter@asterix1 ​- ...jypeter ​42 >​python +# Where is it coming from ('​rpm'​ works on a RedHat-like Linux machine)? 
-Python 2.6.(r266:84292May 22 201508:34:51+ > rpm -qf /​usr/​bin/​python 
-[GCC 4.4.7 20120313 ​(Red Hat 4.4.7-15)] on linux2+python-2.7.5-77.el7_6.x86_64 
 + 
 +# You also get some information when you start the interpreter 
 + > python 
 +Python 2.7.(defaultApr  9 201914:30:50
 +[GCC 4.8.5 20150623 ​(Red Hat 4.8.5-36)] on linux2
 Type "​help",​ "​copyright",​ "​credits"​ or "​license"​ for more information. Type "​help",​ "​copyright",​ "​credits"​ or "​license"​ for more information.
 >>>​ >>>​
 </​code>​ </​code>​
  
-===== Some python distributions ​=====+===== Ultra quick-start on the LSCE servers ​=====
  
-This section will help you choose a distribution in the big Python ecosystem (many distributions,​ python 2.7.*, python 3.*, ...). You should use a distribution that is already installed near your 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!+==== Initialization ====
  
-We suggest ​that you **use Python 2 rather than Python ​3**. Or you can check the [[https://​wiki.lsce.ipsl.fr/​pmip3/​doku.php/​other:​python:​jyp_steps#​python_27_vs_python_3|differences between both versions]] and try to write scripts that will work in both versions! ​8-)+  - Start a terminal on an //obelix// server 
 +  - Type ''​python''​ to access the //default Python 2// interpreter\\ or follow the steps below to use the more complete //CDAT// distribution.\\ Remember ​that **you can exit the interpreter by typing CTRL-D** (or ''​quit()''​ or ''​exit()''​) 
 +    - Determine if you are using a [[other:​newppl:​starting#​which_shell_are_you_using|bash or tcsh shell]], then type the following command to initialize //conda// 
 +      * In a //bash// shell: ''​source ~jypeter/​.conda3_jyp.sh''​ 
 +      * In a //tcsh// shell: ''​source ~jypeter/​.conda3_jyp.csh''​ 
 +    - Choose which //flavor// of Python you want by typing: 
 +      * Python 2.7.x: ''​conda activate cdatm_py2''​ 
 +      * Python 3.x: ''​conda activate cdatm_py3''​ 
 +    - Type ''​python''​ to start the interpreter 
 + 
 +==== 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.cum**TABTAB**'' ​ | 
 +|  **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''​) ​ | 
 + 
 +==== Executing a python script ==== 
 + 
 +Note: Python is an [[https://​en.wikipedia.org/​wiki/​Interpreted_language|interpreted language]] and we speak of //Python scripts// ​rather than //Python ​programs//​. 
 + 
 +  ​''​python'':​ start the interpreter and start working interactively 
 +  ​''​python script.py'':​ execute //​script.py//​ and **exit** 
 +  * ''​python -i script'':​ execute //​script.py//​ and **stay in the interpreter** (''​-i''​ = //​**I**nteractive//​) 
 + 
 +Type ''​man python''​ if you want to see what other command line options are available 
 + 
 +==== Stand-alone script ==== 
 + 
 +You don't have to explicitly call the python interpreter,​ if the interpreter is specified in a ''#​!''​ [[https://​en.wikipedia.org/​wiki/​Shebang_%28Unix%29|shebang comment]] on **the first line of the script** with, and the script has its //execution bit// set (''​chmod +x my_script.py''​).  
 + 
 +If you have a ''​basic_script.py''​ file with the following content in a directory... 
 +<​code>​ 
 +#​!/​usr/​bin/​env python 
 + 
 +import sys 
 + 
 +script_name = sys.argv[0] 
 + 
 +print('​Hello world, I am the ' + script_name + ' script'​) 
 + 
 +# The end</​code>​ 
 + 
 +...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: 
 + 
 +<​code>​ > chmod +x basic_script.py 
 + 
 + > ls -l basic_script.py 
 +-rwxr-xr-x [...] basic_script.py 
 + 
 + > ./​basic_script.py 
 +Hello world, I am the ./​basic_script.py script 
 +</​code>​ 
 + 
 +==== 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:​jyp_steps|JYP'​s recommended steps for learning python]] page 
 + 
 +===== Python distributions available for LSCE users ===== 
 + 
 +This section will help you choose a distribution in the big Python ecosystem (many distributions,​ python version 2.7.* or 3.*, ...) on some of the servers used by LSCE users. 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!) and try to identify who is maintaining it, if you need help or additional packages. 
 + 
 +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! 
 + 
 +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://​wiki.lsce.ipsl.fr/​pmip3/​doku.php/​other:​python:​jyp_steps#​python_27_vs_python_3|differences between both versions]] and try to write scripts that will work in both versions!
  
 ==== LSCE distribution ==== ==== LSCE distribution ====
  
-Contact: the LSCE system administrators+  * Contact: the LSCE system administrators ​([[help-lsce@lsce.ipsl.fr]]) 
 +  * Where: //obelix// interactive servers and cluster at LSCE 
 +  * Initialization type: [[other:​newppl:​starting#​using_module_to_access_optional_programs|module based]] + //conda// 
 +  * What's installed: type ''​conda list''​ after initializing the LSCE distribution
  
 <​code>​ <​code>​
-jypeter@asterix1 - ...jypeter - 43 >module avail+ > module avail
 [...] [...]
-castem/12        ferret/6.9.5     hdf5/1.8.9       netcdf/4p        ​python/​2.7.5 ​    sun-java/7.0.45+4ARTIC/3.6         ​grib_api/1.14      ​netcdf/3           python/​2.7.5 
 +batch_env ​         grib_api/1.14.0    ​netcdf/​4 ​          ​python/​3.
 +[...] 
 +glost/​0.3.1 ​       ncview/​2.1.7 ​      ​python/​2.7
 [...] [...]
  
-jypeter@asterix1 - ...jypeter - 44 >module load python/2.7.5+ > module load python/2.7
  
-jypeter@asterix1 - ...jypeter - 45 >which python + > which python 
-/​usr/​local/​install/​python-2.7.5/bin/python+/​usr/​local/​install/​python-2.7/​bin/​python
  
-jypeter@asterix1 - ...jypeter - 46 >​python + > python 
-Python 2.7.(default, ​Sep 18 201315:47:43+Python 2.7.15 |Anaconda, Inc.| (default, ​Oct 10 201821:32:13
-[GCC 4.4.20120313 (Red Hat 4.4.7-3)] on linux2+[GCC 7.3.0] on linux2
 Type "​help",​ "​copyright",​ "​credits"​ or "​license"​ for more information. Type "​help",​ "​copyright",​ "​credits"​ or "​license"​ for more information.
 >>>​ >>>​
Line 45: Line 137:
 ==== TGCC distribution ==== ==== TGCC distribution ====
  
-Contact: the TGCC hotline+  * Contact: the TGCC hotline ​([[hotline.tgcc@cea.fr]]) 
 +  * Where: CEA TGCC 
 +  * Initialization type: [[other:​newppl:​starting#​using_module_to_access_optional_programs|module based]] 
 +  * What's installed: it depends...
  
-<​code>​ +<​code>​# Get the default version of the Python based modules 
-[p25jype@curie70 ~]$ module avail+$ module avail -t -d | egrep '​(python|cdat)'​ 
 +flavor/​cdat/​standard(default) 
 +flavor/​nest/​python2(default) 
 +flavor/​pytorch/​python2(default) 
 +flavor/​tensorflow/​gpu_python2(default) 
 +intelpython2/​2019.0(default) 
 +intelpython3/​2019.0(default) 
 +python/​2.7.14(default) 
 +python3/​3.6.4(default) 
 +cdat/​8.0(default) 
 + 
 +$ module load python
 [...] [...]
-cmake/​2.8.9(default) ​     hwloc/​1.5 ​                ​netcdf-utils/​4.3.3.1_hdf5 python/​2.7.8 ​             tix/8.4.3 +load module python/2.7.14 (Python)
-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.(Python)+
  
-[p25jype@curie70 ~]$ which python +$ which python 
-/usr/local/​ccc_python/2.7.8_201409/bin/python+/ccc/products/python-2.7.14/​intel--17.0.4.196__openmpi--2.0.2/​default/bin/python
  
-[p25jype@curie70 ~]$ python +$ python 
-Python 2.7.(default, ​Aug 27 201417:50:16+Python 2.7.14 (default, ​Jan 11 201816:43:59
-[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2+[GCC 4.8.5] on linux2
 Type "​help",​ "​copyright",​ "​credits"​ or "​license"​ for more information. Type "​help",​ "​copyright",​ "​credits"​ or "​license"​ for more information.
 >>>​ >>>​
 </​code>​ </​code>​
  
-==== UV-CDAT ====+==== CDAT ==== 
 + 
 +[[https://​uvcdat.llnl.gov/​|CDAT]] (//​Community Data Analysis Tools//) is a powerful and complete front-end to a rich set of visual-data exploration and analysis capabilities well suited for **climate** data analysis problems, and is recommended by JYP. 
 + 
 +CDAT is **available for Mac, Linux and Windows 10 (Windows 10 + [[other:​win10wsl|Windows Subsystem for Linux, and Ubuntu]])**. 
 + 
 +  * Contact: Jean-Yves Peterschmitt (//JYP//) @ LSCE 
 +  * Where: //obelix// interactive servers and cluster at LSCE, //ciclad// interactive servers and cluster at IPSL, irene @ TGCC 
 +  * Initialization type: conda based or [[other:​newppl:​starting#​using_module_to_access_optional_programs|module based]] 
 +  * What's installed: type ''​conda list''​ after initializing a specific CDAT distribution 
 + 
 +=== CDAT versions maintained by JYP === 
 + 
 +The following versions are maintained by JYP, on the Linux servers where LSCE users have accounts 
 + 
 +^  CDAT version ​ ^  JYP\\ version ​ ^  python\\ version ​ ^  Availability? ​ ^  Available packages ^  Installation notes  ^ 
 +|  **8.1** ​ |  18  |  2.7.15\\ 3.6.7  |  LSCE, ciclad ​ |  {{ :​other:​uvcdat:​cdat_conda:​cdat-8.1_py2_list_190307.txt |Default CDAT packages}}\\ [[other:​uvcdat:​cdat_conda:​cdat_8_1#​extra_packages_list|Extra packages]] ​ |  [[other:​uvcdat:​cdat_conda:​cdat_8_1|8.1 notes]] ​ | 
 + 
 +=== CDAT at TGCC === 
 + 
 +CDAT **8.0** is installed at TGCC and can be initialized with: 
 +  * Python 2: ''​module load cdat''​ 
 +  * Python 3: ''​module load flavor/​cdat/​python3 cdat''​ 
 + 
 +=== The basics of CDAT initialization === 
 + 
 +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:​newppl:​starting#​which_shell_are_you_using|Which shell are you using?]] if you are not sure 
 +    * 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! 
 + 
 +<​code>​$ python -c '​import cdms2, vcs' 
 +Traceback (most recent call last): 
 +  File "<​string>",​ line 1, in <​module>​ 
 +ImportError:​ No module named cdms2 
 + 
 +# Am I using the correct python distribution?​ 
 +$ which python 
 +/​usr/​bin/​python</​code>​ 
 + 
 +=== conda-based versions of CDAT === 
 + 
 +<WRAP center round important 60%> 
 +\\ The steps detailed in this section are for [[other:​uvcdat:​cdat_conda:​|UV-CDAT ​2.8.0 and later]] 
 +</​WRAP>​ 
 + 
 +Note: if you need to maintain a **local** version on **your** machine, you can read [[other:​uvcdat:​conda_notes|Installing and maintaining CDAT with conda]] and the [[other:​uvcdat:​cdat_conda:​index#​versions|versions specific notes]]. 
 + 
 +== Configuring .bashrc ​== 
 + 
 +You should **add** the following line at the end of your ''​~/​.bashrc''​ configuration file (create the ''​~/​.bashrc''​ file if it does not exist yet). This will basically make sure that your shell can find the ''​conda''​ executable it needs for initializing CDAT, and define a ''​wp''​ alias that you can use to find where the ''​python''​ executable is located 
 + 
 +^  Server ​ ^  Line to add to ''​.bashrc'' ​ ^ 
 +|  **LSCE** ​ | ''​source ~jypeter/​.conda_jyp.sh''​\\ \\ Note:{{ :​other:​python:​conda_jyp.txt |conda_jyp}} content | 
 +|  **ciclad** ​ | ''​source ~jypmce/​.conda_jyp.sh''​ | 
 + 
 + 
 +Note: when you add the specified line to your ''​bashrc''​ file, you will then always get the python supplied by conda, instead of the system'​s python, when you use ''​bash''​. If you don't like this permanent behavior, just type ''​source ~jyp_login_on_this_server/​.conda_jyp.sh''​ in the bash shell where you want to use conda+CDAT, rather than adding it to the ''​.bashrc''​ file... 
 + 
 +== Initializing CDAT == 
 + 
 +<WRAP center round tip 60%> 
 +When you initialize CDAT, it will be available only in the **current** window/​shell. In the other windows, you still get whatever your default python is. This should minimize potential side-effects 
 +</​WRAP>​ 
 + 
 +  - If [[other:​newppl:​starting#​which_shell_are_you_using|your shell]] is not //bash//, start a bash shell by typing ''​bash''​ 
 + 
 +    * You can later go back to your previous shell by typing ''​exit''​ or ''​CTRL-D''​ 
 +  - Activate the //conda environment//​ that uses the version of UV-CDAT you want to use 
 +    * ''​source activate conda_env_name''​ 
 +    * Use the table below to determine which environments are available on which server 
 +  - Use CDAT ! 
 + 
 +^  UV-CDAT version ​ ^  Server ​ ^  Environment name\\ and activation line  ^  Packages\\ (''​conda list''​) ​ ^ 
 +|  [[:​other:​uvcdat:​cdat_conda:​cdat_2_8_0|2.8.0]] ​ |  **LSCE**\\ **ciclad** ​ |**cdatm14** (''​source activate cdatm14''​) ​ |  {{ :​other:​uvcdat:​cdat_conda:​conda_list_lsce_cdatm14_170310.txt |cdatm14 installed packages}} ​ | 
 +|  [[:​other:​uvcdat:​cdat_conda:​cdat_2_10|2.10]] ​ |  **LSCE** ​ |**cdatm15** (''​source activate cdatm15''​) ​ |  {{ :​other:​uvcdat:​cdat_conda:​uvcdat-2.10_list_170721.txt |cdatm14 installed packages}} ​ | 
 +|  **cdatm** will always point to the latest stable version ​ |||| 
 +|  //latest stable version// ​ |  **LSCE** ​ |**cdatm** (''​source activate cdatm''​) ​ |  ''​conda list'' ​ | 
  
  
 ==== Canopy ==== ==== Canopy ====
  
-[[https://​www.enthought.com/​products/​canopy/​|Enthought Canopy]] is //a Scientific and Analytic Python Deployment with Integrated Analysis Environment//​ provided by [[https://​www.enthought.com/​|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+[[https://​www.enthought.com/​products/​canopy/​|Enthought Canopy]] is //a Scientific and Analytic Python Deployment with Integrated Analysis Environment//​ provided by [[https://​www.enthought.com/​|Enthought]]. It used to be called EPD (//​Enthought Python Distribution//​) 
 + 
 +Canopy ​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 ====
  
-[[https://​www.continuum.io/​why-anaconda|Anaconda]] is a distribution similar to [[#canopy]] provided by [[https://​www.continuum.io/​|Continuum Analytics]]. ​It is available for Windows, Mac and Linux+[[https://​www.continuum.io/​why-anaconda|Anaconda]] is a distribution similar to [[#canopy]] provided by [[https://​www.continuum.io/​|Continuum Analytics]]. 
 + 
 +Anaconda ​is **available for Windows, Mac and Linux** 
 + 
 +Note: Anaconda provides and uses ''​conda''​ for its installation. Since UV-CDAT is now also installed and maintained with ''​conda'',​ you can read the [[other:​uvcdat:​conda_notes|Installing and maintaining UV-CDAT with conda]] page for more information,​ even if you are not going to use UV-CDAT 
 + 
 +===== ipython ===== 
 + 
 +==== 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''​ 
 + 
 +  * [[http://​ipython.readthedocs.org/​en/​stable/​interactive/​tutorial.html|Using IPython for interactive work]] 
 +  * [[https://​damontallen.github.io/​IPython-quick-ref-sheets/​|Ipython-quick-ref-sheets]] 
 + 
 +==== 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 [[other:​python:​jyp_steps#​part_1|introduction to Python, part 1]]). 
 + 
 +Starting the notebook server: ''​ipython notebook''​ 
 + 
 +<note warning>​**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 [[other:​newppl:​starting#​determining_the_load_of_a_linux_server|use the '​top'​ command]] in order to monitor what is happening</​note>​
  
 /* standard page footer */ /* standard page footer */
other/python/starting.txt · Last modified: 2024/05/30 13:41 by jypeter