Setup
The setup will be different from previous assignments as we will use Python and Jupyter Notebook. We assume most of you have some experience with Python, and you won't be working with large existing systems, so there will be a bit less hand-holding when it comes to code here.
Forking Repo
Begin by forking the Assignment 3 repo as with previous assignments.
Virtual Environments
Virtual environments are a development tool used in Python that will keep your project dependencies isolated from other projects. To motivate why we use virtual environments, here's an example that many of you may run into at Cornell. You'll be required to install different versions of Python and packages as you work on assignments for various courses (and perhaps even different projects for the same course). For this class, we may require you to use Python version A and Numpy version B, but you may need to use Python version C and Numpy version D for Computer Vision. Suppose you install all these different versions globally onto your system. In that case, you'll run into dependency issues when the wrong package version is used for the wrong project. Instead, we will be using virtual environments to isolate project dependencies.
We'll be giving instructions on how to use Conda, an open-source package management and environment management system. This means we'll use Conda to create and install packages in those environments.
Installing Conda
You are welcome to use other virtual environment managers (such as venv) if you feel more comfortable with them. However, the course staff is directed to use conda so there is no guarantee they will be able to help you with other managers if you run into issues.
We will be using Miniconda to install conda onto our system.
- Navigate to Latest Miniconda Installer Links
- Download the appropriate installer for your system
- Run the downloaded file to install conda
- Click yes to the minconda installer initialization
- You may need to run
source .bashrc
after miniconda is installed
Once you have conda installed, you'll notice (base) in your command prompt on the terminal. This means you are in the default environment. It's generally bad practice to install packages into the base environment, so we will create our own in the next step...
Option 1. Creating Environment from YAML File (Recommended)
We've provided a YAML environment file that lists all the packages you will need for A3. Navigate to your project directory and run the following:
conda env create -f environment.yml
This will create a conda environment named 4620-A3 but will not activate it! To activate, run the following command:
conda activate 4620-A3
You should see (base) change to (4620-A3) when your environment is activated. You can now install packages into this environment using conda install or pip install (not that you need to since our environment YAML took care of that) and run code using the packages in this environment. When you open the terminal to work on this assignment, you must reactivate your environment!
Make sure to activate your 4620-A3 environment every time you open the terminal. You can verify by checking that your command prompt contains (4620-A3) instead of (base)
Option 2. Manually Creating Environment
If the above isn't working or you choose to create your own environment from scratch then do the following:
conda create -n 4620-A3 python=3.10.0
This will create a conda environment named 4620-A3 with python version 10.0.0. Next activate your environment:
conda activate 4620-A3
With your environment activated install the following packages:
Jupyter notebook, tifffile (Python TIFF library), nb_conda_kernels (allows conda environments to appear in Jupyter):
conda install -c conda-forge notebook tifffile nb_conda_kernels
Pillow (Python imaging library):
conda install -c anaconda pillow
NumPy, nbconvert (converting Jupyter notebooks to other formats), ipywidgets (using interactive widgets within jupyter), matplotlib (plotting library):
conda install numpy nbconvert ipywidgets matplotlib
Running Jupyter Notebook
In the terminal, run the following command from your project directory:
jupyter notebook
It should automatically open a browser window pointing at the Jupyter application. If this doesn't happen, you can just browse to the URL that it prints out. When using Jupyter, all the information is stored in the process running on the terminal. So if you close the window or exit the Jupyter server with control-C, you will lose everything unsaved that is stored in the notebook and any variables that have been defined in the notebook.
This is not often a significant problem since Jupyter auto-saves pretty often. But you need to know that auto-saves are stored in a file in the same directory as the notebook (it's called .ipynb_checkpoints). You need to rerun Jupyter in the same directory to see them. When you hit "Save" in the Jupyter interface, it will update the actual notebook file.
To make sure you are using the virtual environment you created, you need to do the following every time you run jupyter notebook: you run jupter notebook you need to do the following:
- Open a notebook (.ipynb file)
- Select Kernel → Select Change Kernel → Select 4620-A3
Now you should be able to import all the dependencies from your virtual environment!
Every time you run a notebook, make sure your kernel is set to 4620-A3. You can check by looking at the top right of your notebook (just below the logout button), which displays the kernel.