Getting started with Miniforge and Python#

Mara Lampert, July 8th, 2024

Introduction to Python and Miniforge#

This blog post explains what Python and Conda / Miniforge is, and how you can download and setup it on your computer. We will also go through some steps how to get started with Bio-image Analysis.

Note: This is an update of a previous Blogpost.

Why do we need Conda to use Python?

Python is a programming language which is easy to learn and very important in scientific data analysis nowadays.

Conda is a package manager which can be used with Python. It is a software allowing to install other software. Read more about Conda here.

Installation of Miniforge#

Here, I am going to show how to install Miniforge. It comes with everything you need and downloads packages from a community-driven open source software provider called conda-forge. First, you pick the Miniforge installer for your operating system here:

All Mac OS users can now jump here.

Installation on Windows#

When Miniforge finished downloading, follow these steps during the installation:

Click Next:

Click I Agree:

Now you have the option to either install Miniforge3 for Just Me or for All Users. We highly recommend picking Just Me, as the other option requires Administrator priviledges and it can make installing packages more difficult later on.

Install Miniforge3 into the default location:

In the next step we recommend to additionally tick Add Miniforge3 to my Path. If you don’t add it to the Path, Conda would not work from any Terminal Window. Click Install

Click Next at the next window and you arrive here. Click Finish to exit the setup:

Great! You are ready to start coding! 👍 To see how to use Conda, jump here

Installation on Mac OS#

First, you open your Terminal. You can find it using the Spotlight Search and typing Terminal like this:

The opened Terminal should look like this:

Then type these two lines to start the installation

curl -L -O "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh"
bash Miniforge3-$(uname)-$(uname -m).sh

You follow the instructions:

  1. press Enter

  2. read the license instructions (scroll down with the down arrow key) and type yes

  3. confirm the installation path with Enter

  4. IMPORTANT when asked if you want to initialize Miniforge, type yes like here:

When you see this:

you are finished! Close and reopen the Terminal now. Happy coding! 👍

Using Conda#

Now we use Conda by opening the command line. If you are not familiar with the command line yet, you can check out Roberts tutorial here.

To open the Command Prompt in Windows, press the Windows button, type cmd and press Enter. The Mac OS users should already know how to open the Terminal ;-)

When you open the command line, it should look like this:

You are now located in the base environment. It is the default environment and includes beside a Python installation the core Conda libraries and dependencies.

Strong recommendation: Never install any packages into the base environment!

The reason for this is that incompatibilities between packages can occur. Robert demonstrated this here. If this happens in any environment apart from the base environment it is no problem. You can delete the environment, recreate it and start again. If this happens in the base environment, you need to delete and reinstall Miniforge.

Creating a new environment#

You can create a new environment typing the following command into the Command Prompt:

conda create -n my_first_env devbio-napari python=3.9 pyqt 

This will create a new environment with the name my_first_env and with Python version 3.9 installed. Furthermore, the latest version of devbio-napari will be installed in this environment, too. Devbio-napari is a collection of Python libraries and Napari plugins maintained by the BiAPoL team, that are useful for processing fluorescent microscopy image data. Conda will ask you about your permission to download the needed packages with Proceed [y]/n. By hitting Enter you confirm this and conda will download and install the necessary packages.

Recommendation: Create one conda environment for every project you are working on. This allows you to keep an overview on the needed packages for the project, maintaining them and ensure compatibility of the packages.

To activate the environment, type:

conda activate my_first_env

This should lead to the prefix my_first_env appearing at the beginning of the command line:

Starting napari#

Now you can open napari, just type:

naparia 

The opened window in napari should look like this and show the napari-assistant, a panel with common image processing operations.

In case it does not look like this, have a look here.

Working with Jupyter lab#

Jupyter lab is a package which helps to keep a good code overview. It comes with devbio-napari. Just close the napari window and type

jupyter lab

into the command line. A new browser window will open. You can start your first notebook clicking this icon:

../../_images/5_jupyter_11.png

The header of the created notebook will look like this:

Finally, test your notebook by typing

print('Hello World!')

You can run the line by pressing Shift + Enter. Which should you give this output:

Now we can import libraries into out notebook like this:

import seaborn as sns

We can execute the cell typing Shift + Enter and receive this error message:

The error message tells us that seaborn is not yet installed in our conda environment, but we will change this now.

Installing new packages#

If you want to install new packages while working in your Jupyter notebook, you can

  1. Open a new Command Prompt Window (While leaving the other Command Prompt Window open)

  2. Activate your current environment

  3. Install packages

For example if you want to install seaborn, you would need to type:

conda install seaborn 

into the Command Prompt Window. This should currently look like this:

We need to open a second Command Prompt Window to install new packages. While doing so, you can leave the first window open.

Conda will ask you again for confirmation (Enter) to proceed with the installation. If you type N + Enter, the operation will be canceled. If you want to install a specific version of a package you can do it as shown here:

conda install <package name>=version 

It is considered good practice to write down the versions you are using to ensure compatibility between packages and to trace bugs. For further information see also the conda user guide here.

Deactivation or Deletion of an environment#

If you want to deactivate the environment you just need to type:

conda deactivate 

If you screwed up your environment and want to delete it, you can type:

conda env remove -n nameofproject_env

Now you have Miniforge installed, know how to work with conda environments and know about some very important packages. Have fun starting your own Bio-image Analysis project! 👍