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/26 09:57] jypeter Improved the quick start section |
other:python:starting [2019/05/17 08:56] jypeter [Initialization] Improved |
||
---|---|---|---|
Line 35: | Line 35: | ||
- Start a terminal on an //obelix// server | - 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()'') | + | - 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// | - 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 //bash// shell: ''source ~jypeter/.conda3_jyp.sh'' | ||
Line 41: | Line 42: | ||
- Choose which //flavor// of Python you want by typing: | - Choose which //flavor// of Python you want by typing: | ||
* Python 2.7.x: ''conda activate cdatm_py2'' | * Python 2.7.x: ''conda activate cdatm_py2'' | ||
- | * Python 2.7.x: ''conda activate cdatm_py3'' | + | * Python 3.x: ''conda activate cdatm_py3'' |
- Type ''python'' to start the interpreter | - 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 ==== | ==== Useful keyboard shortcuts ==== | ||
Line 268: | 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 ==== |