Installing python packages#

This chapter contains instructions about how to install additional packages useful for scientific programming, such as:

  • ipython and spyder for development

  • jupyterlab for interactive notebooks

  • other packages of the scientific ecosystem, such as numpy, xarray, matplotlib…

Prerequisites#

  • you have a working python installation based on mambaforge (Unit 01) or conda

  • you understand the differences between the windows prompt, the miniforge prompt and the python interpreter

Warning

If you still have doubts about any of these terms, do not hesitate to revisit the installation instructions in Unit 01.

Context: the conda (base) environment#

When you open the miniforge prompt (a terminal in linux/mac), you are opening a windows prompt with new tools available: for one, python is installed and can be run. Similarly, conda and mamba commands are only available from the miniforge prompt, and not from the standard prompt. This is possible thanks to conda, which is a package management system for python. Conda gives you access to a very large number of python packages for free (the only thing you’ll need is an internet connection, to connect to the package servers).

You will recognize that you are using conda thanks to the following signs:

  • when opening the miniforge prompt or terminal, a (base) text appears in front of the current path. For the user Jane, a typical miniforge prompt looks like: (base) C:\Users\Jane>

  • when Jane asks her computer where to find python, conda is indicating the python.exe that came with the conda installation.

You can ask for the location of a specific prompt command with the command where (which in linux). For Jane, the miniforge prompt gives the following indications about the location of python:

(base) C:\Users\Jane> where python 
C:\Users\Jane\mambaforge\python.exe
C:\Users\Jane\AppData\Local\Microsoft\WindowsApps\python.exe

(base) C:\Users\Jane> 

The first python.exe on the list is the one that will be used if you type python in the prompt. This is precisely why conda is useful: it clearly separates your python installation from all other contents on your computer.

Install ipython, jupyterlab and spyder in the (base) environment#

ipython, jupyter, and spyder are fundamental tools of a scientific python installation. We will use them a LOT during the semester and your studies. We will make a demonstration in class, but first you have to install it! From the miniforge prompt, type:

mamba install ipython jupyterlab spyder 

This will install ipython, jupyterlab and spyder at the same time. To check if it worked, type:

ipython

Which should display something like:

Python 3.12.5 | packaged by conda-forge | (main, Aug  8 2024, 18:24:51) [MSC v.1940 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.27.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]:

The only visual difference between the ipython and python interpreters is that >>> has been replaced by In [1]:. More on this later.

Exit ipython (remember how? Use exit()), and then type:

spyder

This should open a development environment called spyder. If all of this worked properly, you are good to go.

Exercise 6

Open a miniforge prompt/terminal and ask windows/linux where to find your current python installation. Compare yours with Jane’s. What about the location of ipython? And of jupyter-lab?

Install other packages and managing environments#

These instructions are useful for later in the class when you will be asked to install more packages and maybe for other classes for which you may have to install packages.

Create an environment called “scipro”#

(base) is the name of the base (default) environment for conda. Installing further packages in (base) is fine, but I recommend against it. I recommend to keep (base) as simple as possible, with few or no packages installed, and use named environments for further usages. I will explain why a bit later.

First, open the miniforge prompt (in base) and type the following command:

mamba create -n scipro --clone base

If asked to confirm, type “yes”.

What did we just do? We created a new conda environment called “scipro” (this is the purpose of the option -n) which clones all packages available in base with the option --clone base (this last part is optional: if you omit it, your new environment will be completely empty and you will have to reinstall jupyter to be able to use it).

You can now activate your new environment with mamba activate scipro.

Exercise 7

Activate the new environment. What changed in comparison to (base)? Now ask the prompt again about where to find the commands ipython and jupyter-lab. Can you see the difference to base?

Conda environments are a very simple and elegant way to manage different installations of python packages. They allow to clearly separate different installations and, more importantly, conda environments allow us to make mistakes.

Since “environments” are nothing else than folders on your computer, they allow setups such as:

  • (base): python v3.10, jupyter-lab, ipython

  • (scipro): same as (base) + numpy, scipy, matplotlib, etc.

  • (test): python 3.11

  • (complex): same as (base) + numpy “beta version” + complicated package

  • etc.

You can switch between environments with mamba activate env_name and leave the current environment with mamba deactivate.

Important

When an environment is active, you can see it with the (base) or (scipro) indicator in front of the prompt. In the active environment, ALL mamba commands refer to this specific environment.

For example, to list the packages available in scipro, you need to activate it first (mamba activate scipro) and then list the packages with mamba list.

To open jupyterlab and have access to the packages installed in scipro, activate the environment first and then start jupyter-lab.

When one of your environments becomes “broken” or obsolete, you can simply delete it with mamba remove -n ENVNAME --all. This will delete the corresponding folder and all packages in it. Creating, activating and deleting environments is super easy, and this is why I recommend their use.

Mamba/conda cheat sheet:

  • mamba create -n scipro --clone base : create an environment called “scipro” with the same packages in it as base

  • mamba create -n scipro : same as above, but empty

  • mamba activate scipro : activate the scipro environment

  • mamba deactivate : leave the current environment

  • mamba info --envs : get a list of all environments

  • mamba list : list the currently installed packages in a specific environment

  • mamba remove -n scipro --all : delete the scipro environment and all packages in it.

Visit the conda documentation for more commands (just replace all “conda” commands with “mamba”).

Installing additional python packages in the active environment#

In the course of your studies, you will need to install many (many) python packages, for example xarray for gridded data analysis or MetPy for meteorology.

Almost always, the install procedure will be:

  1. open the miniforge prompt/terminal

  2. (optional but recommended) activate the environment where you want to install the package

  3. install the package with mamba install

For now, I ask you to install the following python packages:

  • numpy, the fundamental package for scientific computing with Python

  • scipy, fundamental algorithms for scientific computing in Python

  • matplotlib, data visualization with Python

To install these, activate the scipro environment (recommended) or use base, and type:

mamba install numpy scipy matplotlib

Answer “yes” to confirm the installation. Note that mamba will install several additional packages. These automatically installed packages are called “dependencies”: they are required for the other packages to function properly.

To test if the installation worked properly, open an ipython interpreter and type:

In [1]: import numpy as np
In [2]: np.arange(1, 11, 2)

The output should be:

Out[2]: array([1, 3, 5, 7, 9])

Congratulations! You are ready for the rest of the lecture.

Learning checklist#