Installing python packages#
This chapter contains instructions about how to install additional packages useful for scientific programming
Prerequisites#
Context: conda environments#
When you open the miniforge prompt, 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 think you’ll need is an
internet connection, to connect to the package servers).
More details on miniforge and mamba
I asked you to insall miniforge instead of “plain” conda for two main reasons:
miniforge is giving you access to the packages available from the conda-forge, which is a more complete and more up-to-date repository for python packages.
miniforge has
mamba
installed per default.mamba
is a drop-in replacement forconda
, and is significantly faster. You don’t need to know why at this stage: just follow my instructions and you will be fine.
You will recognize that you are using conda thanks to the following indices:
when opening the miniforge prompt, a
(base)
text appears in front of the current path. For 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
.
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.
Open a miniforge prompt and ask windows where to find your current python installation. Compare yours with Jane’s.
What about the location of ipython
? And of jupyter-lab
?
Recommended: create an environment called “inpro”#
(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’ll explain you why a bit later.
First, open the miniforge prompt (in base
) and type the following command:
mamba create -n inpro --clone base
If asked to confirm, type “yes”.
What did we just do? We created a new conda environment called “inpro
” (this is the
purpose of the option -n
) which clones all packages available in base
(this last part is optional: if you omit it,
your new environment will be completely empty and you’ll have to reinstall jupyter to be able to
use it).
You can now activate your new environment with mamba activate inpro
.
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.9, jupyter-lab, ipython(inpro)
: same as(base)
+ numpy, scipy, matplotlib(test)
: python 3.10(complex)
: same as(base)
+ numpy “beta version” + complicated packageetc.
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 (inpro)
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 inpro
, you need to activate it first (mamba activate inpro
) and
then list the packages with mamba list
.
To open jupyterlab and have access the packages installed in inpro
, activate it 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 inpro --clone base
: create an environment called “inpro
” with the same packages in it asbase
mamba create -n inpro
: same as above, but emptymamba activate inpro
: activate theinpro
environmentmamba deactivate
: leave the current environmentmamba info --envs
: get a list of all environmentsmamba list
: list the currently installed packages in a specific environmentmamba remove -n inpro --all
: delete the inpro environment and all packages in it.
Visit the conda documentation for more commands
(just replace all “conda
” commands with “mamba
”).
Required: installing additional python packages#
In the course of your studies, you will be lead to install many (many) python packages, for example xarray for gridded data analysis or MetPy for meteorology.
Almost always, the install procedure will be:
open the miniforge prompt
(optional but recommended) activate the environment where you want to install the package
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 inpro
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#