The Anaconda Python Distribution

Installing the Anaconda Python Distribution with the Spyder 5 IDE and JupyterLab 3 IDE on Windows 11 and Best Practices for Managing a conda Environment

Python beginners are recommended to install Anaconda opposed to installing Python.

What is Anaconda and what is the difference? Anaconda is a Python distribution. The Anaconda Python distribution contains the core Python programming language and its inbuilt modules, the Spyder and JupyterLab Integrating Development Environments (IDEs), and the most common libraries or modules for data science such as numpy, scipy, pandas, matplotlib and seaborn as well as a multitude of others. IDEs can be thought as programs for working with Python code, similar to how one might use Libre Office or Microsoft Word to work with a document.

Why have a Python distribution? Anaconda performs a number of checks for incompatibilities when the user attempts to install additional Python modules. Such compatibilities arise when one module is dependent on another. The seaborn library for example is a plotting library for data visualization and acts as a wrapper around the matplotlib plotting library. The matplotlib plotting library itself is dependent on numpy which is used for array manipulation. When the user blindly installs the latest version of Python and then numpy and then matplotlib and then seaborn incompatibilities are very likely to arise. If an underlying object in numpy is changed or deprecated, this can in turn break the functionality of matplotlib which can in turn break the functionality of seaborn and so on and so forth. This can lead to endless frustrations particularly for a new user who doesn't yet know what they are doing. Such frustrations are alleviated to a great extent by use of Anaconda.

Python alongside the most commonly used data science libraries can be used as an open-source alternative to expensive commercial scientific software such as MATLAB. This guide will focus on installation of Python and at the end link to additional guides which I put together which examine the use of Python to replace MATLAB functionality.

This guide will use Windows 11 but instructions are more or less identical for windows 10 64 Bit. Windows 7 64 Bit is not supported as it has reached End of Life.

If you are instead using Linux (Zorin OS 16 will run better than Windows 10 64 Bit on 2010-2014 hardware) follow the instructions here:

There are usually no installation issues on a clean install of Windows. Installation issues usually arise when there is an old Anaconda installation present. Unfortunately the uninstaller does not purge the system of all the old configuration files which may reintroduce a problem after a reinstallation if they are problematic. I have included additional instructions to uninstall and purge an old installation in this scenario.

Table of contents

  1. Installing the Anaconda Python Distribution with the Spyder 5 IDE and JupyterLab 3 IDE on Windows 11 and Best Practices for Managing a conda Environment
    1. Videos
      1. Uninstalling Anaconda and Kite
        1. Uninstalling Anaconda
          1. Uninstalling Kite
            1. Purging the Anaconda Configuration
            2. Installation
              1. Installing Anaconda
                1. The pip (Python install package) command
                  1. The conda command
                  2. The Anaconda Navigator
                    1. Launching the Anaconda Navigator
                      1. Anaconda Navigator Preferences
                      2. Spyder IDE
                        1. Launching Spyder
                          1. Installation of Kite Coding Assistant
                            1. Spyder Preferences
                              1. Script Editor and Kite Autocompletion
                                1. Help Pane Tab
                                  1. Run Settings and Cells
                                    1. Variable Explorer Tab
                                      1. Files Tab
                                        1. Plots Tab and Plot Settings
                                        2. JupyterLab IDE
                                          1. Launching JupyterLab
                                            1. JupyterLab File Explorer
                                              1. JupyterLab Markdown File
                                                1. Headings
                                                  1. Formatted Text
                                                    1. Spacing
                                                      1. Lists
                                                        1. Tables
                                                          1. Links and Images
                                                            1. Code
                                                              1. Equations
                                                                1. Escape Characters
                                                                2. JupyterLab Notebook File
                                                                  1. Cell Output
                                                                    1. Variables
                                                                      1. Docstrings
                                                                        1. Plots
                                                                        2. Useful Shortcut Keys
                                                                          1. Changing Cell Type
                                                                            1. Running Cells
                                                                              1. Code Autocompletion
                                                                            2. Managing a conda Environment
                                                                              1. base conda environment
                                                                                1. Create a new conda environment
                                                                                  1. Activate a conda environment
                                                                                    1. Install packages to a conda environment
                                                                                      1. Remove a conda environment
                                                                                        1. Cloning a conda environment
                                                                                        2. JupyterLab Lab Extensions
                                                                                          1. jupyter labextension command
                                                                                            1. nodejs
                                                                                              1. Table of Contents/Navigation Pane
                                                                                                1. Interactive Python Widgets
                                                                                                  1. Variable Inspector
                                                                                                    1. Interactive Python matplotlib
                                                                                                      1. plotly
                                                                                                        1. draw.io
                                                                                                        2. Windows Terminal and Windows Environmental Variables Path
                                                                                                          1. Windows Environmental Variable Path
                                                                                                            1. Changing Environment used for Spyder
                                                                                                            2. Additional Tutorials
                                                                                                              1. Python Procedural Programming
                                                                                                                1. Python Code Blocks
                                                                                                                  1. Object Orientated Programming
                                                                                                                    1. Numeric Python
                                                                                                                      1. Data Analysis
                                                                                                                        1. Plotting

                                                                                                                        Videos

                                                                                                                        Uninstalling Anaconda and Kite

                                                                                                                        Uninstalling Anaconda

                                                                                                                        Anaconda can be uninstalled from Apps and Features. Right click the Start Button and select Apps and Features:

                                                                                                                        Select Anaconda3 and then select Uninstall:

                                                                                                                        Select Uninstall:

                                                                                                                        Select Next:

                                                                                                                        Select Uninstall:

                                                                                                                        Select Next:

                                                                                                                        Select Next:

                                                                                                                        Select Reboot Now and then Finish:

                                                                                                                        Uninstalling Kite

                                                                                                                        Kite can also be uninstalled from Apps & Features:

                                                                                                                        Select Uninstall:

                                                                                                                        Accept the User Account Control Prompt:

                                                                                                                        Select Close:

                                                                                                                        Purging the Anaconda Configuration

                                                                                                                        Unfortunately when Anaconda is uninstalled, it leaves a lot of configuration files behind. Sometimes one of these files can be corrupted or have bad settings which will be retained when Anaconda is reinstalled.

                                                                                                                        Press [⊞] and [ e ] to open up file explorer. To the top select Settings → Show → Hidden Items:

                                                                                                                        To get to your user profile type in the following in the Windows Explorer Address Bar:

                                                                                                                        %UserProfile%/

                                                                                                                        Then delete the .conda, .ipython, .jupyter, .matplotlib, .spyder-py3, anaconda3 folders and the .condarc file:

                                                                                                                        Now type the following into the Windows Explorer Address Bar:

                                                                                                                        %AppData$

                                                                                                                        This will take you to the AppData Roaming folder:

                                                                                                                        Delete the .anaconda, jupyter and kite folders:

                                                                                                                        Press [Alt] + [↑] to go up a folder and then select the Local Subfolder instead of the Roaming subfolder.

                                                                                                                        Delete the conda, Kite, pip and Spyder folders (do not delete the Packages folder).

                                                                                                                        Installation

                                                                                                                        Installing Anaconda

                                                                                                                        The Anaconda Individual installer can be downloaded from the Anaconda website:

                                                                                                                        Select Download:

                                                                                                                        Double click the Anaconda installer:

                                                                                                                        Select Next:

                                                                                                                        Select I Agree:

                                                                                                                        Select "Just Me" and then Next:

                                                                                                                        By default an Anaconda3 folder will be created in your user directory. Note installation may fail if there is a space in your user directory. Select Next:

                                                                                                                        Under Advanced options select both Add Anaconda to my PATH environment variable (this option will allow you to access Python directly from the Windows Terminal (PowerShell) opposed to just Anaconda Powershell Prompt) and Register Anaconda3 as my default Python 3.8. The difference between these will be explaned in more depth later on:

                                                                                                                        Select Next:

                                                                                                                        Select Next:

                                                                                                                        You can opt to view the getting started tutorials. Select Finish:

                                                                                                                        The pip (Python install package) command

                                                                                                                        In Python library documentation you will see frequent references to pip (Python install package). When using a conda environment, although the Anaconda Powershell Prompt recognises the:

                                                                                                                        pip install package-to-be-installed

                                                                                                                        command, it is frequently problematic. It should be avoided where possible as there is normally a conda equivalent command that behind the scenes performs additional checks to solve (address any conflicts and check for dependencies) the conda environment.

                                                                                                                        Search for the name of your package on the Anaconda Website before following any other installation instructions involving pip

                                                                                                                        The conda command

                                                                                                                        We will use the conda command to update the Anconda Python Distribution and also install the latest versions of JupyterLab and Spyder released by the JupyterLab and Spyder development teams.

                                                                                                                        Go to the Windows 11 Start Menu and launch the Anaconda PowerShell Prompt:

                                                                                                                        This will open up the Anaconda PowerShell Prompt. To the left hand side in parenthesis the currently selected conda environment will be selected (more details about this later). The default environment is called base:

                                                                                                                        Open up the Anaconda PowerShell Prompt and type in:

                                                                                                                        conda

                                                                                                                        This will give you details about the positional arguments. Most notable are:

                                                                                                                        conda list

                                                                                                                        This will list all the currently installed Python libraries in the currently selected conda environment.

                                                                                                                        The default conda environment is the base conda environment and in the Anaconda PowerShell Prompt we can see that it is selected. We can use the optional argument name to specify the conda environment explicitly (this is recommended good practice, particularly if you are going to use the Windows Terminal; more details about this and conda environments will be discussed later).

                                                                                                                        conda list --name base

                                                                                                                        Because this optional argument is so commonly used it can be abbreviated using:

                                                                                                                        conda list -n base

                                                                                                                        The next positional input argument is update. update has to be followed by the name of the library we wish to update. In the vast majority of cases when update is used the optional argument –all is used which updates everything to the latest versions the Anaconda team have tested and deemed stable.

                                                                                                                        conda update -all

                                                                                                                        You will be told about all the packages being updated. Input

                                                                                                                        y

                                                                                                                        To proceed:

                                                                                                                        Anaconda should now be updated:

                                                                                                                        The next command is search. search by default will search the official conda channel for all versions of a library. Let's use jupyterlab as an example:

                                                                                                                        conda search jupyterlab

                                                                                                                        We see all the versions of jupyterlab we can install. The currently installed version (c.f. the output from conda list) is 3.0.14 which is the latest listed:

                                                                                                                        We can use the optional argument –channel to explicitly specify the repository or channel to search:

                                                                                                                        conda search --channel conda jupyterlab

                                                                                                                        Because channel is so commonly used, it can be abbreviated using:

                                                                                                                        conda search -c conda jupyterlab

                                                                                                                        The conda-forge channel is used by developers of Python IDEs and individual Python libraries. In general the developer submit their latest version to the conda-forge channel and if the product is commonly used it is also later tested by the Anaconda team and added to the conda channel. Not all the Python libraries are mainstream and not all versions of a mainstream library are tested by the Anaconda team for example if the developer releases a version with a bug and in quick succession a newer version to address the bug, the Anaconda team may only test the newer version. There are also some products where development has been stopped on the conda-forge channel and versions are directly released on the conda channel by the Anaconda team. It is usually therefore worthwhile checking both channels for the latest version. Some developers such as plotly also maintain their own channel/repository.

                                                                                                                        conda search -c conda-forge jupyterlab

                                                                                                                        In the example above we see that the latest version on the conda channel is 3.0.14 (currently installed) however there is a newer version 3.0.16 on the conda-forge channel.

                                                                                                                        To install this latest version we can use the install positional argument alongside the optional arguments -n to specify our conda environment and -c to specify the conda forge channel. Usually it also helps to specify the version we wish to install:

                                                                                                                        conda install -c conda-forge -n base jupyterlab=3.0.16

                                                                                                                        The Anaconda Powershell Prompt will take some time to get the package information and attempt to solve the conda environment. Essentially it will look for all the dependencies for JupyterLab 3.0.16 and prompt to install or update other packages to compatible versions which accommodate JupyterLab. It will also check if updating these packages has an influence on any of the other packages installed in the conda environment. You will be warned if incompatible packages are removed or downgraded. Input:

                                                                                                                        y

                                                                                                                        To proceed:

                                                                                                                        The packages will be downloaded and installed and any other stated change in package version will be carried out. You will see a new prompt when the operation is finished:

                                                                                                                        We can repeat the same procedure with Spyder:

                                                                                                                        conda search -c conda spyder

                                                                                                                        At the time of writing the latest version is 5.0.5 on the conda channel:

                                                                                                                        conda search -c conda-forge spyder

                                                                                                                        At this current moment in time, the conda channel is up to date, also giving version 5.0.5:

                                                                                                                        Therefore spyder can be installed using:

                                                                                                                        conda install -c conda -n base spyder=5.0.5

                                                                                                                        If in the future, the conda channel has an older version than conda-forge, install the latest version from conda-forge.

                                                                                                                        conda will attempt to solve the environment and then attempt to use a flexible solve. If this takes an extremely long time press [Ctrl] + [ c ] to cancel the last command. Then retry using the conda-forge channel instead. My speculation when this happens is the conda channel is looking for a dependency that isn't yet uploaded to the conda channel and therefore hangs, this dependency is usually on the conda-forge channel so installation proceeds:

                                                                                                                        The Anaconda Powershell Prompt will give you details about the package information. It will also check if updating these packages has an influence on any of the other packages installed in the conda environment. You will be warned if incompatible packages are removed or downgraded. Input:

                                                                                                                        y

                                                                                                                        To proceed:

                                                                                                                        Spyder 5 will be installed and you will be taken to a new prompt:

                                                                                                                        The Anaconda Navigator

                                                                                                                        The Anaconda Navigator is a simple general user interface which lists all of the Python IDEs installed within the Anaconda Python distribution and has a general user interface for installing packages. In practice however it is very buggy and installing packages via the Anaconda Powershell Prompt is usually more reliable.

                                                                                                                        Launching the Anaconda Navigator

                                                                                                                        You can launch the Anaconda Navigator from the Start Menu:

                                                                                                                        Anaconda comes with a number of IDEs preinstalled. These can be launched by clicking their respective tile and selecting launch. Other IDEs can be Installed via Anaconda and then subsequently launched.

                                                                                                                        This guide will only focus on two IDEs, Spyder and JupyterLab. Unfortunately (at this time of writing), the Anaconda Navigator hangs when launching Spyder from its tile (Spyder can be launched via its own Start Menu Tile and by use of the Anaconda Powershell Prompt):

                                                                                                                        Anaconda Navigator Preferences

                                                                                                                        The Anaconda Navigator has a high DPI setting however it doesn't handle High DPI screens with a Display Scaling at 200 % very well. In my XPS 13 9365 the Tiles are too large to move. If you have this issue you can try and select File → Preferences:

                                                                                                                        In theory you should be able to uncheck the High DPI Settings and select Apply:

                                                                                                                        On my XPS 13 9365, this dialog window was too big and I could not get to these options. If you are in this situation, close Anaconda Navigator. Open file explorer by pressing [⊞] and [ e ] and then in the address bar type in:

                                                                                                                        %AppData%

                                                                                                                        Select the .anaconda, then navigator and then .anaconda and then navigator subfolder. Right click anaconda-navigator configuration settings and select open with notepad:

                                                                                                                        Change the line:

                                                                                                                        enable_high_dpi_scaling = False

                                                                                                                        You should be able to relaunch the Anaconda Navigator and see the tiles at normal size.

                                                                                                                        The anaconda navigator can be launched from the anaconda powershell prompt by typing:

                                                                                                                        anaconda-navigator

                                                                                                                        Spyder IDE

                                                                                                                        Launching Spyder

                                                                                                                        Spyder can be launched from its own start Menu shortcut, from its tile in the Anaconda Navigator (not working on my install) or from the Anaconda PowerShell Prompt by typing in:

                                                                                                                        spyder

                                                                                                                        When you first launch Spyder you will get a welcome tour:

                                                                                                                        Installation of Kite Coding Assistant

                                                                                                                        If you haven't already been prompted by Spyder to install Kite. Select Tools → Install Kite Autocompletion Engine:

                                                                                                                        Then select Install Kite:

                                                                                                                        The Spyder IDE will download the Kite Setup:

                                                                                                                        If you get an error, download Kite Manually and begin the installation:

                                                                                                                        Once downloaded the installation will hang until the User Account Control Prompt is selected:

                                                                                                                        Accept the User Account Control Prompt:

                                                                                                                        Kite should prompt you to optionally log in with an email. Select continue:

                                                                                                                        Select Let's Go:

                                                                                                                        Kite will then perform a background check for Python IDE plugins:

                                                                                                                        If it doesn't find any, it will prompt you to install editor plugins:

                                                                                                                        To install the Spyder IDE plugin you will have to close Spyder:

                                                                                                                        Once Spyder is closed, select Install:

                                                                                                                        The Spyder Kite plugin should now be installed:

                                                                                                                        A Kite tutorial will open up when you launch Spyder:

                                                                                                                        Spyder Preferences

                                                                                                                        To change your Spyder Preferences, select Tools and Preferences:

                                                                                                                        Then under Appearance you can optionally change from the default Spyder Dark Syntax Highlighting theme to the Spyder (Light) Syntax highlighting theme. I will also increase the text size slightly (so screenshots are easier to read). Once you have made your changes, select Apply:

                                                                                                                        You will be prompted to restart Spyder in order to apply the changes:

                                                                                                                        You will see Spyder relaunch with the Spyder Light Syntax Highlighting Theme:

                                                                                                                        You can return to Preferences and change the Editor settings. For beginners it is recommended to Show Blank Spaces and Indent Guides as spacing within Python is important:

                                                                                                                        Script Editor and Kite Autocompletion

                                                                                                                        To the left hand side is the Spyder script Editor. This can be used to write a script and has a tabbed interface so you can work with multiple script files:

                                                                                                                        To save your script file, select File → Save As…

                                                                                                                        Then select your file location (the convention is to use folders that have lower case folder names without spaces):

                                                                                                                        Then rename the Python Script File, the convention is to use lower case names without spaces for script files, note that the .py extension will be added automatically:

                                                                                                                        Now the Address Bar and the name of the tab reflect the file path of the script file and the name of the script file respectively:

                                                                                                                        When you type in the script editor, Kite Autocompletion will pop up displaying suggestions:

                                                                                                                        When a library is imported, items from it can be accessed by typing the library name followed by the dot . syntax. A Kite dropdown list pop up will display of the objects:

                                                                                                                        For a function or class, details about the input arguments will be shown when the function or class is typed with open parenthesis as a pop up:

                                                                                                                        Unfortunately at present the auto-completion is substantially delayed for the commonly used datascience libraries such as numpy, matplotlib and pandas. The following autocompletiiton takes about 5-10 s to load on my machine:

                                                                                                                        Help Pane Tab

                                                                                                                        Some classes or functions have a tool tips which can be expanded in the Help Pane:

                                                                                                                        Objects can also be highlighted in the script editor and the shortcut key [Ctrl] + [ i ] can be pressed to inspect their documentation. This opens in the Help Pane.

                                                                                                                        Run Settings and Cells

                                                                                                                        We can create a very basic script which will print the str "Hello World" to the console:

                                                                                                                        print("Hello World")

                                                                                                                        When the first script is run, the run settings dialog will display. I will leave all these settings at default and select Run. These settings can later be changed by selecting Preferences and then Run:

                                                                                                                        You will see the print statement, printed to the console:

                                                                                                                        Beside the Run Button, is the Run Cell Button. The popup dialog informs us that we can create new cells using #%%:

                                                                                                                        We can create a script with cells using:

                                                                                                                        #%% Cell 1
                                                                                                                        print("Hello World")
                                                                                                                        #%% Cell 2
                                                                                                                        print("Goodbye Cruel World") 

                                                                                                                        Note the currently selected cell is highlighted in yellow. The run cell button rns the currently selected cell and this cell is still highlighted after the cell is ran:

                                                                                                                        The button beside this, runs the currently selected cell and then moves onto the next cell:

                                                                                                                        The button to the right of this is the Run Selection button:

                                                                                                                        This is the equivalent of running a single line in the console.

                                                                                                                        Variable Explorer Tab

                                                                                                                        Spyder has a Variable Explorer which makes it easy to visualise data. The variable name, type, size and value are shown. Different datatypes are colour-coded. For collections such as list, dict, numpy arrays and DataFrame, the variables can be expanded and visualised in more details:

                                                                                                                        #%% Imports
                                                                                                                        import numpy as np
                                                                                                                        import pandas as pd
                                                                                                                        #%% Create Variables (Builtin Python)
                                                                                                                        a = "Hello"
                                                                                                                        b = 1
                                                                                                                        c = 2.1
                                                                                                                        d = True
                                                                                                                        e = [1, 2, 3, 4, 5]
                                                                                                                        f = {"fruit": ["apple", "banana", "pear"],
                                                                                                                             "quantity": [2, 2, 1],
                                                                                                                             "calories_per_unit": [52, 89, 57]}
                                                                                                                        #%% Create Variables (Datascience Libraries)
                                                                                                                        g = np.array(e)
                                                                                                                        h = pd.DataFrame(f)

                                                                                                                        Files Tab

                                                                                                                        A basic Excel SpreadSheet Book1.xlsx can be saved into the same folder as the Python Script spyder_test.py:

                                                                                                                        The Current Working Directory is the folder where the last script run is stored. In this case, the Excel SpreadSheet and the Python Script show in the Files Tab:

                                                                                                                        We can use the pandas function read_excel to read this Excel File into a DataFrame because it is in the same folder as the script file i.e. relative to the script file, we only need to specify the file name and not the full file path:

                                                                                                                        #%% Imports
                                                                                                                        import numpy as np
                                                                                                                        import pandas as pd
                                                                                                                        #%% Create Variables (Builtin Python)
                                                                                                                        a = "Hello"
                                                                                                                        b = 1
                                                                                                                        c = 2.1
                                                                                                                        d = True
                                                                                                                        e = [1, 2, 3, 4, 5]
                                                                                                                        f = {"fruit": ["apple", "banana", "pear"],
                                                                                                                             "quantity": [2, 2, 1],
                                                                                                                             "calories_per_unit": [52, 89, 57]}
                                                                                                                        #%% Create Variables (Datascience Libraries)
                                                                                                                        g = np.array(e)
                                                                                                                        h = pd.DataFrame(f)
                                                                                                                        #%% Read xlsx File
                                                                                                                        i = pd.read_excel("Book1.xlsx")

                                                                                                                        Plots Tab and Plot Settings

                                                                                                                        We can use the plot function within the matplotlib pyplot module to create a plot:

                                                                                                                        #%% Imports
                                                                                                                        import numpy as np
                                                                                                                        import pandas as pd
                                                                                                                        import matplotlib.pyplot as plt
                                                                                                                        #%% Create Variables (Builtin Python)
                                                                                                                        a = "Hello"
                                                                                                                        b = 1
                                                                                                                        c = 2.1
                                                                                                                        d = True
                                                                                                                        e = [1, 2, 3, 4, 5]
                                                                                                                        f = {"fruit": ["apple", "banana", "pear"],
                                                                                                                             "quantity": [2, 2, 1],
                                                                                                                             "calories_per_unit": [52, 89, 57]}
                                                                                                                        #%% Create Variables (Datascience Libraries)
                                                                                                                        g = np.array(e)
                                                                                                                        h = pd.DataFrame(f)
                                                                                                                        #%% Read xlsx File
                                                                                                                        i = pd.read_excel("Book1.xlsx")
                                                                                                                        plt.plot(i.x, i.y)

                                                                                                                        By default it displays as an inline plot (static plot) in the Plots tab:

                                                                                                                        We can also use interactive plots which have their own dedicated plot window. To get automatic plots, select Tools → Preferences:

                                                                                                                        To the left hand side select Ipython Console and to the top select Graphics. Then change the Backend to Automatic. Then select Apply:

                                                                                                                        For changes to take place, the Kernel needs to be restarted. Select Consoles → Restart Kernel. This will clear all Variables and close all imported modules:

                                                                                                                        Select Yes:

                                                                                                                        Plots in the plots pane will remain but can be removed:

                                                                                                                        Now when the script is ran, the plot displays as an Automatic Plot and has its own dedicated Window:

                                                                                                                        JupyterLab IDE

                                                                                                                        The JupyterLab IDE is browser based and the Notebook format within JupyterLab has some advantages over the script files used in Spyder as it includes the markdown syntax which essentially allows one to create a Notebook with executable code. In other cases, this Notebook format is a disadvantage over writing pure Python code in Spyder using script files for example in the creation of a script for an automated process or a Python module.

                                                                                                                        Launching JupyterLab

                                                                                                                        The JupyterLab IDE has no Start Menu shortcut and must be launched either from its tile within the Anaconda Navigator or via a command line in the Anaconda Powershell Prompt. To launch jupyterlab we use the command:

                                                                                                                        jupyter-lab

                                                                                                                        Note the dash – which doesn't get used when using the conda install, search, update commands.

                                                                                                                        JupyterLab File Explorer

                                                                                                                        To the left hand side is the JupyterLab file explorer. This can be used to select a folder and open a file.

                                                                                                                        Alternatively one can right click an empty space to create a new folder or right click a folder/file to rename them.

                                                                                                                        To the top left is the launcher. A launcher is already open by default and can be used to create a new Text File, Markdown File or Notebook File. Additional launchers can be opened in separate tabs to work with multiple files simultaneously.

                                                                                                                        We can right click the file (or the tab name) and select rename:

                                                                                                                        In this example the file is renamed and the updated file name shows in the opened tab:

                                                                                                                        The File Menu has the usual save options.

                                                                                                                        If we go to the File Explorer in Windows 11, we will see that our text file exists and can be opened in Notepad.

                                                                                                                        All of the files which we will save will display within Windows Explorer but to open them we will usually launch JupyterLab and use the JupyterLab file explorer to open the file opposed to the Windows 11 File Explorer (which won't find a program to open the file extension).

                                                                                                                        JupyterLab Markdown File

                                                                                                                        It is fast to write text to a text file but we have next to no formatting capability. The markdown file allows us to intuitively create a file with some basic formatting. We can launch a new Launcher and then create a New Markdown File and rename it, just like we did with the text file:

                                                                                                                        We can right click some blank space in the Markdown file and view the Markdown Preview:

                                                                                                                        Headings

                                                                                                                        We can prefix text with # to create a Heading. The number of # we prepend, assigns the level of Headings:

                                                                                                                        # Heading 1
                                                                                                                        ## Heading 2
                                                                                                                        ### Heading 3
                                                                                                                        #### Heading 4

                                                                                                                        JupyterLab has a navigation pane and the headings will auto-populate this as they are created.

                                                                                                                        Formatted Text

                                                                                                                        The * can be used to enclose text. Enclosure by a single * makes the text italic, enclosure by two * makes the text bold and enclosure by three * makes the text bold-italic. Enclosure by two ~~ makes the text strike-through.

                                                                                                                        Let's make a sentence with *italic text*, **bold text**, ***bold-italic*** and ~~strike-through~~ text.

                                                                                                                        Spacing

                                                                                                                        Text on a new line will automatically be joined together with text on the line before to form a paragraph.

                                                                                                                        She sells
                                                                                                                        seashells
                                                                                                                        on the
                                                                                                                        seashore

                                                                                                                        To explicitly specify text output on a new line, we need to use a blank line:

                                                                                                                        She sells
                                                                                                                        
                                                                                                                        seashells
                                                                                                                        
                                                                                                                        on the
                                                                                                                        
                                                                                                                        seashore

                                                                                                                        Lists

                                                                                                                        Bullet Point List:
                                                                                                                        * one
                                                                                                                        * two
                                                                                                                        * three
                                                                                                                        
                                                                                                                        Bullet Point List (spaced):
                                                                                                                        * one
                                                                                                                        
                                                                                                                        * two
                                                                                                                        
                                                                                                                        * three
                                                                                                                        
                                                                                                                        Numeric List:
                                                                                                                        1. one
                                                                                                                        2. two
                                                                                                                        3. three
                                                                                                                        
                                                                                                                        Numeric List (spaced):
                                                                                                                        1. one
                                                                                                                        
                                                                                                                        2. two
                                                                                                                        
                                                                                                                        3. three
                                                                                                                        

                                                                                                                        Tables

                                                                                                                        We can use the pipe to create a table, row by row. The first row is the column names and the second row is the column formats (which can be changed to normal, left aligned, right aligned, left-aligned with title centred):

                                                                                                                        |num|number|
                                                                                                                        |---|---|
                                                                                                                        |1|one|
                                                                                                                        |2|two|
                                                                                                                        |3|three|
                                                                                                                        
                                                                                                                        |num|number|
                                                                                                                        |---|:-|
                                                                                                                        |1|one|
                                                                                                                        |2|two|
                                                                                                                        |3|three|
                                                                                                                        
                                                                                                                        |num|number|
                                                                                                                        |---|-:|
                                                                                                                        |1|one|
                                                                                                                        |2|two|
                                                                                                                        |3|three|
                                                                                                                        
                                                                                                                        |num|number|
                                                                                                                        |---|:-:|
                                                                                                                        |1|one|
                                                                                                                        |2|two|
                                                                                                                        |3|three|

                                                                                                                        We use square brackets to enclose a link text, followed by parenthesis to enclose a link:

                                                                                                                        [Anaconda Individual Edition](https://www.anaconda.com/products/individual)

                                                                                                                        The link can be to an image:

                                                                                                                        [Anaconda Logo](https://www.anaconda.com/imager/assetsdo/Content/individual-edition-2021-05/47600/AIE-2021-Navigator_69c3a6a2ae8373ad29ab2c2b8be2395d.png)

                                                                                                                        We can display an image within the markdown file by prepending it with an exclamation mark:

                                                                                                                        ![Anaconda Logo](https://www.anaconda.com/imager/assetsdo/Content/individual-edition-2021-05/47600/AIE-2021-Navigator_69c3a6a2ae8373ad29ab2c2b8be2395d.png)

                                                                                                                        If the image file is stored locally on your computer, you can use the file path of the image. Image files in the same folder as the Notebook file can be specified directly without the full file path.

                                                                                                                        Code

                                                                                                                        Three back quotes can be used to enclose an inline piece of code. They can also be used to create a code block:

                                                                                                                        The first line of code is ```"Hello World"```.
                                                                                                                        
                                                                                                                        The code block is:
                                                                                                                        
                                                                                                                        ```
                                                                                                                        print("Hello World")
                                                                                                                        print("Goodbye World")
                                                                                                                        ```
                                                                                                                        
                                                                                                                        It prints.

                                                                                                                        Equations

                                                                                                                        Inline equations are enclosed in a single $ and display equations are enclosed in a double $. The equations themselves must be written in LaTeX. This guide will use the Microsoft Word Equation Editor to copy an equation to LaTeX to use as an example:

                                                                                                                        The equation is $f\left(x\right)=a_0+\sum_{n=1}^{\infty}\left(a_n\cos{\frac{n\pi x}{L}}+b_n\sin{\frac{n\pi x}{L}}\right)$
                                                                                                                        
                                                                                                                        The equation is:
                                                                                                                        
                                                                                                                        $$f\left(x\right)=a_0+\sum_{n=1}^{\infty}\left(a_n\cos{\frac{n\pi x}{L}}+b_n\sin{\frac{n\pi x}{L}}\right)$$

                                                                                                                        Escape Characters

                                                                                                                        We have used #, *, ~, |, `, $ characters for format specification. If we want to include one of these as part of our markdown text, we need to insert them as an escape character. To do this we prepend the escape character with \ and when \ is the escape character to be inserted we use \\.

                                                                                                                        To make text bold we use \***bold**\*.
                                                                                                                        
                                                                                                                        To insert a backslash we use \\.

                                                                                                                        JupyterLab Notebook File

                                                                                                                        Now that we have an overview of markdown we can now create a JupyterLab notebook file and rename it as we seen before with the text file and markdown file.

                                                                                                                        Unlike Spyder where the use of Code Cells were optional, in a JupyterLab notebook file Cells are mandatory. Each Cell can be ran as either a Code Cell or Markdown Cell. For example the code we ran in Spyder before can be included in a Notebook:

                                                                                                                        # Python Notes
                                                                                                                        Importing the DataScience Libraries.
                                                                                                                        import numpy as np
                                                                                                                        import pandas as pd
                                                                                                                        import matplotlib.pyplot as plt
                                                                                                                        # Inbuilt Example DataTypes
                                                                                                                        You should know:
                                                                                                                        
                                                                                                                        * str
                                                                                                                        * int
                                                                                                                        * float
                                                                                                                        * bool
                                                                                                                        * list
                                                                                                                        * dict
                                                                                                                        a = "Hello"
                                                                                                                        b = 1
                                                                                                                        c = 2.1
                                                                                                                        d = True
                                                                                                                        e = [1, 2, 3, 4, 5]
                                                                                                                        f = {"fruit": ["apple", "banana", "pear"],
                                                                                                                             "quantity": [2, 2, 1],
                                                                                                                             "calories_per_unit": [52, 89, 57]}

                                                                                                                        In JupyterLab by default, each Cell is configured to be a Code Cell. The currently selected cell will be indicated in blue to the left hand side. To the top we can select the Cell Type dropdown list and change the Cell Type to a Markdown Cell:

                                                                                                                        Notice the change in the Syntax highlighting which makes the Cell look more similar to the Markdown text shown in the .md file:

                                                                                                                        Cell Output

                                                                                                                        Instead of a separate Markdown Preview we Run each Cell. When we run a Markdown Cell it displays in an equivalent manner to the Markdown Preview.

                                                                                                                        Note that double clicking on the displayed Markdown Text will revert it back to a Markdown Cell which you can modify and Run again.

                                                                                                                        When we run a Code Cell we Execute the Code (similar to the way we ran a Cell within Spyder). The number which displays to the left hand side of the Code Cell indicates the order each Cell is Run and is equivalent to the number in the Prompt in the Spyder Console. Note that the number will increment each time a Cell is Run and Cells do not need to be Run in order. If a Cell is Run again only the most recent number will display to the left hand side:

                                                                                                                        Instead of having a Console, each Code Cell has a Cell output.

                                                                                                                        Variables

                                                                                                                        When a value is not assigned to a variable it will be output at the bottom of the Cell:

                                                                                                                        1 + 2

                                                                                                                        When a value is assigned to a variable it will not be output at the bottom of the cell (unless the print statement is used):

                                                                                                                        b = 1 + 2

                                                                                                                        Docstrings

                                                                                                                        The Docstring of a function or class can be output to a cell using the question mark before typing in the function or class name:

                                                                                                                        ? round

                                                                                                                        For long Docstrings, the Cell Output can be converted to a scrolling output by right clicking the cell output.

                                                                                                                        Plots

                                                                                                                        We can access a file stored in the same location as the Notebook File. For convenience we will copy the Excell File we created before to this location:

                                                                                                                        i = pd.read_excel("Book1.xlsx")

                                                                                                                        We can create a plot of the DataFrame i:

                                                                                                                        plt.plot(i.x, i.y)

                                                                                                                        It will display as an inline plot by default:

                                                                                                                        We can change the plot backend to automatic, to instead plot the plot in a dedicated Window:

                                                                                                                        %matplotlib auto

                                                                                                                        Rerunning the Code Cell that creates the plot will update the Cell Output to inform you that the plot is Automatic and this plot will display in a seperate Window:

                                                                                                                        Notice that the Number of the Prompt to the left hand has now be updated from 7 to 9, so we can easily tell that this Code Block has been Run after the Setting for Automatic Plotting was applied. To change the backend back to inline use:

                                                                                                                        %matplotlib inline

                                                                                                                        We can left click the side of the Code Block beside the [8] and if we hold down the left click we can drag it to reposition it:

                                                                                                                        For example we can update the Notebook to specify the matplotlib backend immediately after importing matplotlib near the top of the Notebook:

                                                                                                                        Useful Shortcut Keys

                                                                                                                        Changing Cell Type

                                                                                                                        [Esc] + [ m ] will change the currently selected Cell Type from a default Code Cell to a Markdown Cell.

                                                                                                                        Running Cells

                                                                                                                        There are two shortcut keys to Run a Cell.

                                                                                                                        [Shift] + [Enter] will run a Cell and move onto the next Cell:

                                                                                                                        [Alt] + [Enter] will Run the Cell and select a blank Cell below it:

                                                                                                                        Code Autocompletion

                                                                                                                        In order to see the list of available objects that can be called from a module or a class (which we can think of as a container). We have to type in dot . (to look in the container) and then we can type in [Tab] to view a dropdown of objects within the module or class (a list of all the items within the container):

                                                                                                                        We can also type in a function or class with open parenthesis followed by a [Shift] and [Tab] to view the Docstring as a pop up balloon to quickly view details about the input arguments:

                                                                                                                        Managing a conda Environment

                                                                                                                        In this example we will create a conda environment.

                                                                                                                        base conda environment

                                                                                                                        The base conda environment is found in:

                                                                                                                        %UserProfile%/Anaconda3

                                                                                                                        This folder contains the python.exe:

                                                                                                                        It also contains a lib folder which contains standard Python libraries:

                                                                                                                        And this contains a site-packages folder which contains the additional Python libraries installed by Anaconda:

                                                                                                                        You will see a numpy, pandas, matplotlib, seaborn, spyder, jupyterlab folders listed here:

                                                                                                                        Create a new conda environment

                                                                                                                        In the base conda environment you will see a folder called envs:

                                                                                                                        %UserProfile%/Anaconda3/envs

                                                                                                                        This folder is going to be empty by default.

                                                                                                                        A conda environment is essentially a sub installation, we can install a specific Python version, a specific set of Python libraries required for a project.

                                                                                                                        We can create a new conda environment by using:

                                                                                                                        conda create -n testenv

                                                                                                                        Where testenv is the name of the conda environment.

                                                                                                                        To proceed input:

                                                                                                                        y

                                                                                                                        You will see the new folder testenv appear which is largely empty because this is an empty conda environment:

                                                                                                                        Activate a conda environment

                                                                                                                        Notice that each prompt in the Anaconda Powershell Prompt begins with (base), this denotes that the base environment is selected. Any conda command such as conda list, conda install .. and conda update … will list, install and update the base installation. To select (or activate) another conda environment we can use:

                                                                                                                        conda activate testenv

                                                                                                                        Notice how the Anaconda Powershell Prompt now begins with (testenv), this means any conda command such as conda list, conda install .. and conda update … will list, install and update the testenv installation.

                                                                                                                        Note when the Anaconda Powershell Prompt is closed and relaunced, base will automatically be reselected.

                                                                                                                        Note that the Windows Terminal does not begin with the (base) or (testenv) and the activate command does not work in Windows Terminal which uses the Anaconda Environment specified in the Windows Environmental Variable Path (more details later).

                                                                                                                        Install packages to a conda environment

                                                                                                                        The conda environment is blank. We can install Python to it using:

                                                                                                                        conda install -n testenv python

                                                                                                                        As testenv is activated in the Anaconda Powershell Prompt, the following line of code will be equivalent to the above:

                                                                                                                        conda install python

                                                                                                                        It is however recommended to explicitly specify your conda env when getting started in order to prevent confusion.

                                                                                                                        Or we can explicitly search and specify a version, as we have seen previously when we updated JupyterLab and Spyder (in the base environment).

                                                                                                                        The Package Plan details will display. To the top the currently selected conda environment will display. Python and its dependencies will be listed. Input:

                                                                                                                        y

                                                                                                                        to proceed with the install.

                                                                                                                        You will now see the folder begin to get populated:

                                                                                                                        It will contain its own Python.exe:

                                                                                                                        It will contain a lib folder with standard libraries and site-packages subfolder:

                                                                                                                        We can install some datascience libraries to this folder:

                                                                                                                        conda install -n testenv numpy
                                                                                                                        conda install -n testenv pandas
                                                                                                                        conda install -n testenv matplotlib
                                                                                                                        conda install -n testenv seaborn

                                                                                                                        We will also need to install an IDE to this folder in order to interface with Python in this conda environment. For example we can install JupyterLab using:

                                                                                                                        conda install -n testenv jupyterlab

                                                                                                                        For clarity we will stick to the conda channel which installs version 3.0.14:

                                                                                                                        The Anaconda Powershell Prompt will list JupyterLab and all its dependencies, we can input:

                                                                                                                        y

                                                                                                                        To proceed.

                                                                                                                        We can launch JupyterLab as normal:

                                                                                                                        jupyterlab

                                                                                                                        Notice that the conda environment is selected so JupyterLab will be launched from %UserProfile%/Anaconda3/envs/testenv/lib/site-packages/jupyterlab and use packages in %UserProfile%/Anaconda3/envs/testenv/lib

                                                                                                                        If we select Help → About JupyterLab:

                                                                                                                        We see the version is 3.0.14 and not 3.0.16 (in the conda base install):

                                                                                                                        The previously opened file is still open. We can select Kernel → Restart Kernel and Run all Cells:

                                                                                                                        Then select Restart at the dialog:

                                                                                                                        Notice that some of the Cell Code Blocks fail to execute. This is expected as we have only installed a subset of the datascience modules we will get a module not found error if for example we attempt to use read_excel from the pandas library because this relies on the openpyxl library which is not installed in this conda environment:

                                                                                                                        This can of course be installed in the Anaconda Powershell Prompt by selecting the environment testenv and installing openpyxl i.e.

                                                                                                                        conda activate testenv
                                                                                                                        conda install -c conda openpyxl

                                                                                                                        Remove a conda environment

                                                                                                                        We can use the env optional argument with remove to remove a conda environment:

                                                                                                                        conda env remove -n testenv

                                                                                                                        Doing so will remove all the packages but leave some empty folders behind. Once the env has been removed, the testenv folder may be deleted within Windows Explorer:

                                                                                                                        Cloning a conda environment

                                                                                                                        Instead of creating a conda environment from scratch and manually installing all the datascience libraries, we can clone the existing conda base environment and then later add packages to it. We will clone the base conda environment to an environment named labextenv by using:

                                                                                                                        conda create -n labextenv --clone base

                                                                                                                        JupyterLab Lab Extensions

                                                                                                                        JupyterLab Lab Extensions can be used to increase the functionality of JupyterLab. Lab Extensions are made by third party developers and unfortunately at present can be a bit problematic and lead to a situation where JupyterLab does not work correctly…

                                                                                                                        We will therefore use the conda environment labextenv to install Lab Extensions so if something goes wrong we can safely revert back to the base environment.

                                                                                                                        conda activate labextenv

                                                                                                                        jupyter labextension command

                                                                                                                        JupyterLab is supposed to have a labextension command which is used for installing extensions when JupyterLab is used outwith a conda environment. Like pip (Python install package), the labextension command is recognised by the Anaconda Powershell Prompt but can be problematic and should be avoided in preference for a conda install command equivalent where possible:

                                                                                                                        jupyter labextension install @name-of-repository/name-of-extension

                                                                                                                        It normally results in an issue when it comes to building JupyterLab resulting in the extension being disabled making it unusable.

                                                                                                                        As a consequence the most commonly used extensions have been uploaded to the conda-forge repository and these install correctly with the Anaconda Powershell Prompt.

                                                                                                                        jupyter lab build

                                                                                                                        These conda install instructions are unfortunately not mentioned on the GitHub page however you can search conda website for more details:

                                                                                                                        nodejs

                                                                                                                        JupyterLab Lab Extensions use the nodejs framework. The latest version of this should be installed before installing any extension.

                                                                                                                        Search the conda and conda-forge channels for the latest version of nodejs. At the time of writing the latest version is 16.5.0 on the channel conda-forge so I will install this:

                                                                                                                        conda search -c conda nodejs
                                                                                                                        conda search -c conda-forge nodejs
                                                                                                                        conda install -c conda-forge -n labextenv nodejs=16.5.0

                                                                                                                        Table of Contents/Navigation Pane

                                                                                                                        This widget now comes preinstalled as part of JupyterLab, you should not follow any instructions on older guides to attempt to install it.

                                                                                                                        Interactive Python Widgets

                                                                                                                        The interactive Python widgets (ipywidgets) are preinstalled with Anaconda. These can be used for the creation of interactive sliders and displays.

                                                                                                                        Variable Inspector

                                                                                                                        JupyterLab by default is sadly missing the Variable Explorer exhibited within the Spyder IDE. The JupyterLab Variable Inspector is an extension that gives JupyterLab a basic equivalent:

                                                                                                                        conda search -c conda-forge jupyterlab-variableinspector
                                                                                                                        conda install -c conda-forge -n labextenv jupyterlab-variableinspector=3.0.9

                                                                                                                        Once enabled you can right click on a blank space within the Notebook and select Open Variable Inspector:

                                                                                                                        The Variable Inspector will open in a new tab:

                                                                                                                        However you can drag and drop the tab to change the layout:

                                                                                                                        The Variable Inspector just lets you view the Variables, it does not allow you to fully interactively Explore them like the Spyder Variable Explorer:

                                                                                                                        Interactive Python matplotlib

                                                                                                                        Earlier we seen we could either have a dedicated interactive plot within its own Windows or a static plot nested within the cell output of the notebook using the matplotlib backends automatic and inline respectively.

                                                                                                                        The interactive Python matplotlib (ipympl) notebook extension can be used for an additional matplotlib backend called widget which nests the plot interactively within the notebook:

                                                                                                                        conda search -c conda-forge ipympl
                                                                                                                        conda install -c conda-forge -n labextenv ipympl=0.7.0

                                                                                                                        plotly

                                                                                                                        The plotly extension can also be used allowing for browser based interactive plots using the plotly library. For instalaltion plotly have their own channel called plotly.

                                                                                                                        conda search -c plotly plotly
                                                                                                                        conda install -c plotly -n labextenv plotly=5.1.0

                                                                                                                        More details and the test code above can be seen on the getting started guide on the plotly website:

                                                                                                                        draw.io

                                                                                                                        The draw.io extension adds a diagram tab to the launcher. You can use draw.io to draw a diagram. Unfortunately it does not include support for draw.io files within a JupyterLab notebook so you will have to convert the draw.io diagram file into an image in order to display it within a JupyterLab Notebook:

                                                                                                                        conda search -c conda-forge jupyterlab-drawio
                                                                                                                        conda install -c conda-forge -n labextenv jupyterlab-drawio=0.9.0

                                                                                                                        Windows Terminal and Windows Environmental Variables Path

                                                                                                                        Windows Environmental Variable Path

                                                                                                                        During installation of Anaconda, you will get the option to "Add Anaconda3 to my PATH environmental variable":

                                                                                                                        This means you should be able to right click your Start Button and select Windows Terminal:

                                                                                                                        And the commands python, conda, spyder and jupyter-lab should all be recognised within the Windows Terminal. For example:

                                                                                                                        python --version

                                                                                                                        If you get a Python not found error, it wasn't added to the Windows Environment Path during installation and needs to be manually added (see below).

                                                                                                                        If a folder is created within Documents called windows_terminal_test. We can select this in the terminal by using:

                                                                                                                        cd Documents
                                                                                                                        cd windows_terminal_test

                                                                                                                        We can create a python test file in notepad called test_script.py (note the .py extension and not the default .txt extension). To run this test script in the terminal we can type:

                                                                                                                        python test_script.py

                                                                                                                        As well as using Python we should be able to use any of the Python libraries installed within our base conda environment:

                                                                                                                        While the Windows Terminal looks at first glance a lot like the Anaconda PowerShell Prompt note the omission of (base) to the left hand side. The Windows Terminal uses the conda environment specified in the Windows Environmental Variables Path and the command conda activate command does not change this so this command essentially does not work within the Windows Terminal.

                                                                                                                        To view the Windows Environmental Path, we can right click the Start Button and select Settings:

                                                                                                                        Then to the left hand side, select System and scroll down to the bottom and select About: