Part 1. Environment set up with conda#

This section provides instructions on how to set up a Python programming environment using Conda, a popular package and environment management system. It also covers the installation and usage of Jupyter Notebook and Jupyter Lab, which are widely used tools for interactive computing and data analysis.

1.1 Install Python via Conda#

The recommended way to install Python and the necessary packages for this course is via Anaconda.

Anaconda is a free and open-source distribution of Python programming language for scientific computing, data science, machine learning, and large-scale data processing. It comes with a package manager called conda that simplifies the installation and management of packages and their dependencies.

Conda is:

  • Package manager: It allows you to easily install, update, and manage software packages and their dependencies.

  • Environment manager: It enables you to create and manage isolated environments with different versions of Python and packages, which is useful for avoiding conflicts between projects.

The main advantages of using conda are:

  • Easy installation of packages: avoids using multiple tools to manage and install packages.

  • You will be installing packages that are pre-compiled and optimized for performance, avoiding compiling packages from source.

  • It allows having isolated environments for different projects, avoiding conflicts between packages and their dependencies.

  • It can be used with multiple programming languages, including Python, R, and Julia.

In this link you will find detailed instructions on how to install Conda based on your operating system (Windows, MacOS, Linux).

1.2 Conda environments#

With conda, you can create, export, list, remove, and update environments that have different versions of Python and/or packages installed in them. This is especially useful when working on multiple projects that require different versions of packages or Python itself.

1.2.1 Create a new conda environment#

Once you have installed Conda, we will create a new environment dedicated for this course. Then you can install all the necessary packages in this environment without affecting other projects you may be working on.

Open a terminal (or Anaconda Prompt on Windows) and run the following command:

conda create -n pyqm python=3.10

This command creates a new conda environment named pyqm with Python version 3.10. You can replace pyqm with any name you prefer for your environment.

We can create the environment with all the necessary packages for this course by running:

conda create -n pyqm python=3.10 numpy scipy matplotlib jupyterlab notebook -y

This command creates a new conda environment named pyqm with Python version 3.10 and installs the required packages. Flag -n specifies the name of the environment, and we list the packages we want to install after specifying the Python version. We can also specify the version of each package if needed, for example numpy=1.21.0.

The -y flag automatically confirms the installation of the packages. If you don’t include this flag, conda will ask for confirmation before proceeding with the installation. In that case, just press y and Enter to confirm.

You can find more detailed information about creating and managing conda environments in the official documentation.

1.2.2 Activate and deactivate a conda environment#

Then, we will need to activate the environment we just created. With Anaconda (or Miniconda) installed, open a terminal window if you are on MacOs or Linux. If you are on Windows, open the Anaconda Prompt (Anaconda3). You can find it by clicking on the Windows logo key and typing Anaconda.

Then run the following command in your terminal:

conda activate pyqm

This command activates the pyqm environment. You should see the environment name in your terminal prompt, indicating that you are now working within that environment. If you get an error, you can print a list of available environments by running conda info --envs. Sometimes you may need to ativate the environment with the full path, e.g. conda activate /path/to/env/pyqm.

conda info --envs
conda activate /path/to/env/pyqm

To deactivate the environment and return to the base environment, simply run:

conda deactivate

You can check wether you have the environment activated by having a look at the terminal prompt. If the environment is activated, you should see the environment name in parentheses before the prompt, like this: (pyqm) user@machine:~$. Otherwise, it will be written as (base) user@machine:~$.

Example:

(base) user@machine:~$ conda activate pyqm 
(pyqm) user@machine:~$ conda deactivate 
(base) user@machine:~$ 

1.2.3 Additional packages#

Throughout the development of your projects, you may need to install additional packages in your conda environment. You can install as many packages as you need in your conda environment. To do so, you can use the conda install <package_name> command.

First, make sure that your specific environment is active. For example, to install the qutip package, you would run:

conda install qutip

Alternatively, you can use pip to install any packages you might need either they are or not available via conda. For example, to install the Jupyter notebook package using pip, you would run:

pip install qutip

You can specify the version of the package you want to install by appending =<version_number> to the package name. For example, to install version 4.6.0 of the qutip package, you would run:

conda install qutip=4.6.0

or

pip install qutip==4.6.0

1.3 Jupyter Notebook and Jupyter Lab#

Jupyter Notebook and Jupyter Lab are open-source web applications provinding a simple and interactive interface for creating and sharing documents that contain live code, equations, visualizations, and narrative text. It supports many programming languages, including Python, Java, R, Julia, Matlab, Octave, Scheme, Processing, Scala, and many more. For this reason, they are nowdays widely used in data science, scientific computing, and machine learning for interactive data analysis and visualization.

1.3.1 Install Jupyter Notebook and Jupyter Lab#

If you followed the instructions in section 1.2.1, you should already have Jupyter Notebook and Jupyter Lab installed in your conda environment. If not, you can install it by running the following command in your terminal (make sure your conda environment is activated):

conda install notebook 
conda install jupyterlab 

The Jupyter Notebook interface is simpler and more straightforward, while Jupyter Lab offers a more flexible and powerful environment with additional features such as multiple tabs, drag-and-drop functionality, and a file browser. In the following days you can explore their capabilities and choose the one that best suits your needs.

1.3.2 Launch Jupyter Notebook and Jupyter Lab#

To launch Jupyter Notebook or Jupyter Lab, make sure your conda environment is activated, then run one of the following commands in your terminal:

jupyter notebook

or

jupyter lab

This will open a new tab in your default web browser with the Jupyter interface. You can then create new notebooks, open existing ones, and start coding.

1.3.3 Create a new notebook#

To create a new notebook, click on the “New” button on the top right corner of the Jupyter interface and select “Python 3” (or the version of Python you have installed in your environment) from the dropdown menu. This will open a new tab with an empty notebook where you can start writing and executing code.

1.3.4 Basic operations in Jupyter Notebook and Jupyter Lab#

Some basic operations you can perform in Jupyter Notebook and Jupyter Lab include:

  • Adding and deleting cells

  • Running cells (Shift + Enter)

  • Changing cell types (Code, Markdown, Raw)

  • Saving notebooks (Ctrl + S or Command + S)

  • Using keyboard shortcuts for faster navigation and execution

1.3.5 Install additional packages in Jupyter Notebook and Jupyter Lab#

If you need to install additional packages while working in a Jupyter Notebook or Jupyter Lab, you can do so by running shell commands directly from a code cell.

You can install packages using either conda or pip. Just make sure your conda environment is activated before launching Jupyter Notebook or Jupyter Lab.

To install a package using conda, you can use the following syntax:

!conda install package_name -y

To install a package using pip, you can use the following syntax:

!pip install package_name

1.3.6 Save and export notebooks#

To save your notebook, simply click on the “Save” icon in the toolbar. Jupyter automatically saves your work periodically, but it’s a good practice to save manually as well.

You can export your notebook to various formats, such as HTML, PDF, or Python script, by clicking on “File” in the menu bar and selecting “Download as” followed by the desired format.

1.3.7 Shutdown Jupyter Notebook and Jupyter Lab#

To shutdown Jupyter Notebook or Jupyter Lab, you can either close the browser tab and then stop the server in the terminal by pressing Ctrl + C, or you can click on “File” in the menu bar and select “Shut Down” to stop the server.

Short note on Jupyter-book-based manuals and textbooks#

This course is based on a series of online manuals and textbooks created with Jupyter Book. Jupyter Book is an open-source tool that allows you to build publication-quality books and documents from computational material.

Each section of this manual is written in a Jupyter Notebook, which allows us to have both markdown cells to, for example, write text and equations that can be rendered in LaTeX, and code cells to write and execute Python code. Therefore, installing this package will allow you to download the textbook and run the cells, or simply copy and paste the code for execution. To install this package, run the following line in the terminal prompt with your conda environment activated:

conda install jupyter-book

More information and guidelines about making online manuals or textbooks with Jupyter Notebooks will be provided in the next weeks.

Summary#

  • Install Conda

  • In a terminal window, create a new environment

    conda create -n pyqm python=3.10 numpy scipy matplotlib jupyterlab notebook -y

  • Activate and deactivate the new environment

    conda activate pyqm

    conda deactivate

  • Install extra packages

    conda install <package_name>

  • Run Jupyter Notebook or Jupyter Lab

    jupyter notebook or jupyter lab

Additional resources#