Python FAQ

Workshop: using Python at IAC

Slides of the Using Python at IAC workshop (11.12.2019)

General

ALERT! Warning: do not automatically activate an environment in .bashrc

Also check the docs on JupyterHub!

JupyterHub

Should I use Python?

Tutorials

Which version should I use? python 2 or python 3?

What's the recommended way to use python at IAC?

What about other solutions?

What are Best Practices when using python?

Why is it important to work with a fixed environment?

Using Conda (on Linux)

What is conda?

What is mamba?

What is a conda environment?

What is a conda package?

How do I use conda?

What environments are available?

Older environments

  • 2022 environments
    module load conda/2022
    conda env list
    
    • iacpy3_2022: based on iacpy3_2021, uses python 3.9; updated all packages on 16.03.2022;

  • 2021 environments
    module load conda/2021
    conda env list
    
    • iacpy3_2021: based on iacpy3_2020, uses python 3.9; updated all packages on 22.04.2021;

  • 2020 environments
    module load conda/2020
    conda env list
    
    • iacpy3_2020: based on iacpy3_2019, uses python 3.7; updated all packages on 15.04.2020;

  • 2019 environments
    module load conda/2019
    conda env list
    
    • iacpy3_2019: based on iacpy3_2018, uses python 3.7; updated all packages on 08.04.2019;
    • iacpy_cmip6_ng environment used to create the cmip6 new generation archive, please use iacpy3_2019

  • 2018 environments
    module load conda/2018
    conda env list
    
    • iacpy3_2018: based on dypy, uses python 3.6; updated all packages on 03.04.2018;
    • iacpy2_2018: based on dypy, but uses python 2.7; updated all packages on 03.04.2018

  • 2017 environments
    module load conda/2017
    conda env list
    
    • dypy: python3.5 environment with dypy (for LAGRANTO) and suitable for most users
    • cis_env: python3.5 environment with cis tools in version 1.5.4
    • pyferret_env: python3.5 environment with pyferret in version 7.0
    • pyn_env: python2.7 environment with PyNgl and PyNio in version 1.5

  • ALERT! module load miniconda3 is equivalent to module load conda

I am missing a package - what can I do?

How can I install a single package?

How can I create my own environment?

Create a new environment

Clone and tweak an existing environment

Background information about the differences between pip, conda and anaconda

How can I create an executable python script when using a conda environment?

How to best run jupyter notebook on a server?

  • See below how to run a notebook on a server from your personal computer.
  • Here we to setup a jupyter notebook running on a server and use it from your computer.
  • It will make use of tmux on the server to keep a session running even if you are not logged in anymore.
  • For security purpose the jupyter notebook produces a token at start, you need to copy this token (password) to the login screen of the notebook.

  • On the server
    tmux new-session -s 'background jobs'
    cd ~
    module load conda/<year>
    source activate <environment>
    jupyter notebook --no-browser --port 55000
    
    • ALERT! If the port is already in use jupyter will select the next higher available. Make sure that you use the right port in the next part.

  • On your computer:
    ssh -f -N -L localhost:8888:localhost:55000 SERVER
    

  • Open the browser and go to: http://127.0.0.1:8888
  • Copy the token given by the jupyter notebook on the server and paste it in the login field.
  • TIP This also works to tunnel a notebook from a linux machine to a windows machine
    • e.g. via (WSL [windows subsystem for linux]), or Start -> cmd
  • TIP You can recover the full address including the token on the SERVER by pressing CTRL C once.

Run a notebook on a server - from your personal computer

  • ALERT! Please don't run jupyter/ python on fog or fog2. These are login nodes that don't have many ressources.

You cannot directly ssh to our servers. Therefore, the above solution does not work from you personal computer. There are two possibilities.

1) Configure fog as a 'jumphost'

  • Edit (or create) ~/.ssh/config file as follows (on your personal computer)
    Host fog
      User <username> # your ETH username
      Hostname fog.ethz.ch
    
    Host atmos # change this according to the SERVER you want to use
      User <username> # your ETH username
      Hostname atmos.ethz.ch # change this as well
      ProxyCommand ssh -q -W %h:%p fog
    

  • now the following command should work
    ssh -f -N -L localhost:8888:localhost:55000 atmos
    
  • if you try to access the server from Windows using putty, make sure that you add the forwarded port to the profile for your session (on the left under "Category" go to "Connection" -> "SSH" -> "Tunnels" and type "8888" into source port, "localhost:55000" into destination, then click "Add")
  • ALERT! do NOT write atmos.ethz.ch, only atmos

2) Use ssh from the USYS VPN

  • Connect to the USYS-VPN
  • You can now ssh to the following servers (i.e. the above script works directly)
    • litho, cfc
  • If you want to access another server, please contact iac-linux@env.ethz.ch

Run spyder remotely

Spyder 3 shows strange symbols

user packages no longer available in conda environment and JupyerHub

Why was this change introduced?

  • Conda environments should be self-contained and reproducible. Site packages 'pollute' the clean workspace (i.e. there could be packages in an environment that were never installed into it).
  • You don't need --user to install packages with conda+pip.
    • Most often you will be able to install packages directly with conda (conda install <package>)
    • When using conda+pip it is not necessary to install packages with --user; you can do this with pip install <package>

I'am missing my self-installed packages, what can I do?

  • Recommended: You should re-install the packages into your enviroment:
    module load conda
    source activate <environment>
    pip install <package>
    
  • NB: Make sure that pip is included in the environment before installing extra packages into it.
  • If you installed site packages into an existing environment (one managed by IAC-IT), you will have to clone this environment, see instructions above.
  • Not recommended: You can get the old behaviour back by adding the following line to your ~/.bashrc (for bash):
    module load conda # as before
    unset PYTHONNOUSERSITE
    

What has changed?

  • module load conda now does the following: export PYTHONNOUSERSITE=1. This avoids putting the site-packages in the pythonpath, as explained in the documentation.


Packages

xarray

How do I prevent xarray from automatically adding _FillValue to coordinates or variables without NaN?

Why did xarray change the type of an nc attribute to string, causing other software to crash on the resulting nc file?

matplotlib

I don't see a figure when typing plt.show()?

There are two ways to achieve this:

More information can be found here: http://matplotlib.sourceforge.net/users/customizing.html

spacer