This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
other:python:starting [2019/04/25 16:07] jypeter Lots of changes |
other:python:starting [2019/05/17 08:56] jypeter [Initialization] Improved |
||
---|---|---|---|
Line 29: | Line 29: | ||
>>> | >>> | ||
</code> | </code> | ||
+ | |||
+ | ===== Ultra quick-start on the LSCE servers ===== | ||
+ | |||
+ | ==== Initialization ==== | ||
+ | |||
+ | - Start a terminal on an //obelix// server | ||
+ | - Type ''python'' to access the //default Python 2// interpreter available on the servers... | ||
+ | - ...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 | ||
+ | - Note: if you will have to use python regularly, you should add the ''source'' line above to your ''.cshrc'' /''.login'' file (//tcsh// users) or ''.bashrc''/''.profile'' (//bash// users) | ||
+ | * **Do not** add the ''conda activate cdatm_pyN'' line to the shell config files, because this will create potential side-effects!\\ Only type this activation line in the terminals where you will need to use this specific version of 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.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 ===== | ===== Python distributions available for LSCE users ===== | ||
Line 199: | Line 271: | ||
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 | 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 | ||
- | ===== Launching Python ===== | + | ===== ipython ===== |
- | + | ||
- | Once you have initialized the [[#some_python_distributions|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 or by clicking a shortcut on your desktop). | + | |
- | + | ||
- | Remember that on Linux/Mac 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.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'') | | + | |
- | + | ||
- | ==== 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 ''#!'' [[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''). | + | |
- | + | ||
- | <code> | + | |
- | 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 | + | |
- | </code> | + | |
- | + | ||
- | ==== 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** (''-i'' = //**I**nteractive//) | + | |
- | + | ||
- | Type ''man python'' if you want to see what other command line options are available | + | |
==== ipython interpreter ==== | ==== ipython interpreter ==== |