Python packages#

Installing packages (Windows)#

Ok, we are presuming some previous knowledge, but to help you get past some hurdles experienced, see tips below:

  • If you try to run some code with an “import” line, and you get an error, this is likely because you don’t have the package installed (on your computer) and python is trying to load it.

Some packages you will need:

  • Gibbs Seawater Toolbox (GSW) - see installation instructions here

  • xarray - see installation for xarray and necessary other packages (dask, netCDF4, bottleneck). This is a very useful data format/package for netcdf files (many ocean/climate datasets).

You may also need:

  • pandas - simpler than xarray. Avoid, but may be needed.

  • numpy - multi-dimensional arrays

  • matplotlib - plotting tools, similar to Matlab style

  • pycnv - for importing Seabird format *.cnv datafiles

Potentially of interest:

Installing packages (Mac)#

We need various packages as above. For environment management, we use conda (or mamba, and their lightweight forms: miniconda and micromamba). Some students also use pipenv, which you can explore on your own e.g. here.

See also

Managing environments with conda or mamba.

Choose a directory where you will work on the python for this course. Navigate to the directory in a terminal window. The basic command is something like conda create --name <my-env> where you replace <my-env> with the name of your environment. Let’s call it messfern_env. But we’ll try to install a lot of the necessary pieces at once:

Create and then activate an environment messfern_env#

conda create --name messfern_env 
conda install --channel conda-forge xarray gsw python pandas gsw numpy scipy cartopy matplotlib jupyterlab nb_conda ipykernel nb_conda_kernels 
conda activate messfern_env

Note

To install an individual package with conda, you can use

conda install -c conda-forge <package>

where conda-forge is the “channel” of packages, meaning that specifying the name “anaconda” will try to find the package you’re requested at a specified location or channel. Another channel you might use is “anaconda”.

We need a few more packages that seem to only have pip instructions

pip install pycnv
conda env export > environment.yml

Note that you should be within an activated conda environment before running the pip install command.

This has created a list of the current environment which you can get from this repository environment.yml (repository). If you download this into your working directory, you can activate the environment:

conda env create -f environment.yml

Then before you start working, activate the environment using

conda activate messfern_env
jupyter-lab

Now, when you start jupyter-lab you’ll see that you can select the environment messfern_env. Or, when you open an existing python notebook (*.ipynb file), in the upper right corner you can select the environment (e.g., messfern_env and tick the box to always open with preferred kernel).

Note

If it’s been a while since you installed conda, you can update it with

conda update -n base -c conda-forge conda

But your conda will tell you when you need to do this

Note

If you want to recreate your environment with conda, after adding a few things, you can do a

conda deactivate
conda remove --name messfern_env --all

And then start fresh with the conda create code above, appending the extra packages in the intial install.

Problems with jupyter-book and kernels#

My jupyter-book didn’t know about my environment messfern_env. This should not be a problem for students, but is a problem when i want to execute this book (the course notes) which contain packages that aren’t in my base installation of python on my computer.

pico $HOME/.jupyter/jupyter_config.json

and add the lines

{
  "CondaKernelSpecManager": {
    "kernelspec_path": "--user"
  }
}

After saving this, I was able to run

python -m nb_conda_kernels list

and it appeared to add my kernels.

Then making the jupter book worked without errors.

(seaocn_env) 18:20 ~/Library/Mobile Documents/com~apple~CloudDocs/Work/teaching/SeaOcn-UHH $ python -m nb_conda_kernels list

Environments#

More advanced: It’s best to work inside an environment. This controls the version of each of the packages that you have installed, so that the code is more likely to run smoothly on another person’s computer. Some background on conda environments.

Troubleshooting#

Not finding kernels in jupyter, try

jupyter kernelspec list

On a mac, this is reading from ~/Library/Jupyter/kernels. In the directories (named according to the environment name) there is a kernel.json file.