Installing python packages
Installing python packages#
This chapter contains instructions about how to install additional packages useful for scientific programming
Context: conda environments#
When you open the miniforge prompt, you are opening a windows prompt with new tools available:
python is installed and can be run. Similarly,
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.
mambainstalled per default.
mambais a drop-in replacement for
conda, 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:
when Jane asks her computer where to find python, conda is indicating the
python.exethat came with the conda installation.
You can ask for the location of a specific prompt command with the command
For Jane, the miniforge prompt gives the following indications about the location of
(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>
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
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
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
jupyter-lab. Can you see the difference to
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 package
You can switch between environments with
mamba activate env_name and leave the current environment with
When an environment is active, you can see it with the
(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
To open jupyterlab and have access the packages installed in
inpro, activate it first and then start
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 as
mamba create -n inpro: same as above, but empty
mamba activate inpro: activate the
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 inpro --all: delete the inpro environment and all packages in it.
Visit the conda documentation for more commands
(just replace all “
conda” commands with “
Required: installing additional python packages#
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
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 : import numpy as np In : np.arange(1, 11, 2)
The output should be:
Out: array([1, 3, 5, 7, 9])
Congratulations! You are ready for the rest of the lecture.