Coding Setup Guide¶

This guide explains how to set up a Python virtual environment and run a Jupyter Notebook in VS Code. You can alternatively use Google collab to run the notebeooks.

If you are new to Python programming, the MIT course 6.0001 Introduction To Computer Science And Programming In Python is a nice intro into coding principles with Python.


0. Prerequisites¶

Open a terminal and make sure that Python is installed. Create a folder (say computational_bioengineering and inside intro for the 1st lecture) and cd into it. For example, on a mac, it should then look like this:

Picture of a terminal

Next make sure you download the course material of the 1st lecture into the folder (in mac you can just open a finder window in the folder via open . in the terminal and then dropping the files into this window).

The most important file is the jupyter notebook file (link on our website). If you also want to have all the figures nicely shown then we recommend downloading the notes.zip (from the website) and unpacking it into the intro folder. The zip includes the jupyter notebook file.

1. Create a Virtual Environment¶

We will use venv to create a virtual Python environment. Any package that you install in it will be only installed to the virtual environment. This prevents your other projects (and your system's Python) to be messed up. We install it via:

python -m venv venv

Note: on older systems python does not work and you need to run python3 instead.

This creates a folder venv/ with your isolated Python environment.


2. Activate the Virtual Environment¶

Let's activate the virtual environment - from then on the python sees only the packages installed in the venv (which is non in the beginning):

  • macOS/Linux:
    source venv/bin/activate
    
  • Windows (PowerShell):
    .\venv\Scripts\activate
    

Your terminal prompt should now start with (venv) and look like this:

Terminal with venv activated

By the way if your colors don't look like this, no worries. We are using a certain terminal style which is not necessary for the course.


3. Install VS Code and open the intro folder¶

You can do this via the offical website

The start it and open the intro folder (that contains the jupyter files and the venv directory).

Then click on the jupyter file (notes.ipynb). This will look like:

VS Code opened


4. Install a Jupyter kernel¶

Click 'Run all' in VS Code (= the 2 right arrow symbol at the top in the image before).

Your VS Code will now complain that it does not know which Python environment it should use to run the code.

It will look like this:

VS Code: select Python environment

Choose 'Python environment' (since we use a local Python environment to run the code).

Then choose the (recommended) 'venv' environment that we created before like here:

VS Code: select Python environment

It will complain that it does not have a Jupyter kernel yet in this environment. Just say 'install':

VS Code: install kernel

And now it should run the notebook in your venv environment.


5. Installing packages¶

If you look closer some of the code will have errors. This is due to missing installed packages (in fact we just have the Jupyter kernel installed and no other packages in our fresh venv). The errors will look like:

VS Code: package missing error

So let's install them. In fact for the 1st lecture we will need 3 packages: numpy, matplotlib, and mobspy.

In Jupyter packages can be installed by running special (so called magic) commands that start with a '%'. We just add in any cell:

%pip install numpy
%pip install matplotlib
%pip install mobspy

And click on 'run all' again (you can also just run this single cell of course).

That will install the packages:

VS Code: package installing

Afterwards don't forget to comment out the lines again - otherwise it will always try to install the packages whenever you run this cell.

VS Code: package being installed


6. You made it¶

Try out some smaller Python programs by adding some Python cells and try changing some code to get familiar with coding.

Happy hacking!