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 Both sides next revision
other:python:starting [2019/04/26 07:35]
jypeter Added Ultra quick-start on the LSCE servers
other:python:starting [2019/04/26 09:57]
jypeter Improved the quick start section
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 for a more advanced ​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 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 ​python +    - 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''​ 
-      ​In a //tcsh// shell: ''​source ~jypeter/​.conda3_jyp.csh''​+      ​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 2.7.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 ===== ===== Python distributions available for LSCE users =====
other/python/starting.txt · Last modified: 2024/05/30 13:41 by jypeter