Configure the Python Environment on Windows with Anaconda and Jupyter Notebook

The easiest way to manage Python libraries is to use Anaconda. So, the first step is to download and install Anaconda. Select your desired Windows version here: https://www.anaconda.com/distribution/

During the installation process, you can avoid to add Anaconda to the PATH manually. Once Anaconda has been installed, locate it: open “Anaconda Prompt” and type

where conda

So, for example, you can locate conda.exe in C:\ProgramData\Anaconda3\Scripts. Add this path to the environment variable PATH, as well as C:\ProgramData\Anaconda3\, which contains the command line python command.

At this point we can update Anaconda:

conda update conda

In order to update all packages, run “Anaconda Prompt” as administrator and then type:

conda update --all

To install a specific package (it doesn’t do anything if already installed):

conda install pandas-datareader

Jupyter Notebook

This is an interactive way to build Python applications. From the “Anaconda Prompt” type:

jupyter notebook

This opens a tab in a browser. A server is running on port 8888 of the localhost. Press Ctrl-C to shut it down. The running tab allows to see and shutdown all running notebooks (it’s not enough to close the browser tab, but you have to manually shut them down).

In order to create a notebook, click on NEW, and then select an available notebook (in my case, Python 3). Before you create the notebook, be sure you are pointing to the correct directory, where you have all the date you need, so that the notebook is created there.

Once you write a command, click SHIFT-ENTER to execute it and see the result. If we do not want to create a new cell, we execute the code with CTRL-ENTER. In case the cell contains many expressions, all of them are evaluated, but only the last one is printed in the output. In addition, this is an independent environment. We do not need to execute cells in order!

Note: when you are on a cell, if you press ESC, you enter in command mode (the cell gets a blue border). In this mode, you can use the keyboard to run shortcuts. For example, pressing A adds a new cell above the current one (Y=code cell, M=markdown cell, H=help, list all shortcuts!). Clicking on the cell, you come back in edit mode.

In order to import a library (downloaded as package), we can execute:

import pandas

We can also assign an alias or import just a submodule:

import pandas as pd import matplotlib.pyplot as plt pd.__version__