2 Setting-up Python Computing Environment
2.1 Use Google Colab
All you need is a Google account. Sign in your Google account in a browser, and navigate to Google Colab. Google Colab supports both Python
and R
. Python
is the default engine. Change the engine to R
in Connect
->change runtime type
. Then you are all set. Your file will be saved to your Google Drive or you can choose to send it to your GitHub
account (recommended).
2.1.1 How to run a project file from your Google Drive?
Many times, when you run a python file in Colab, it needs to access other files, such as data files in a subdirectory. In this case, it would be convenient to have the same file structure in the Google Colab user home directory. To do this, you can use Google Drive to store your project folder, and then mount the Google Drive in Colab.
Let’s assume the project folder name, pydata-book/
.Here are the steps:
git clone
the project folder (example:git clone https://github.com/wesm/pydata-book.git
) to your local folder. This step is only needed when you want to clone some remote repo from GitHub.- Upload the folder (ex:
pydata-book
) to Google Drive. - Open the file using Colab. In Google Drive, double click on the
ipynb
file, example,ch06.ipynb
(or click on the three dots on the right end, and chooseopen with
, thenGoogle Colaborotary
), the file will be opened by Google Colab. - Mount the Google Drive. In Google Colab, with the specific file (example,
ch06.ipynb
) being opened, move your cursor to the first code cell, and then click on the folder icon (this should be the fourth icon) on the upper left border in the Colab browser. This will open the file explorer pane. Typically you would see a folder namedsample_data
shown. On the top of the pane, click on the Google Drive icon to mount the Google Drive. Google Colab will insert the following code below the cursor in your openedipynb
file:
from google.colab import drive
drive.mount('/content/drive')
Run this code cell by pressing SHIFT+ENTER
, and follow the prompts to complete the authentication. Wait for ~10 seconds, your Google Drive will be mounted in Colab, and it will be displayed as a folder named drive
in the file explorer pane. You might need to click on the Refresh
folder icon to see the folder drive
.
5. Open a new code cell below the above code cell, and type the code
%cd /content/drive/MyDrive/pydata-book/
This is to change the directory to the project directory on the Google Drive. Run this code cell, and you are ready to run the file ch06.ipynb
from the folder pydata-book
on your personal Google Drive, just like it’s on your local computer.
2.2 On your own computer
Anaconda: Download anaconda and install using default installation options
VSC: Download VSC and install
start VSC and install VSC extensions in VSC: Python, Jupyter, intellicode
(optional) Quarto for authoring: Download Quarto and install
Start an anaconda terminal. Navigate to the file directory.
Setup a conda virtual environment: stat1010 and install python and ipykernel engines
conda create -n stat1010 python ipykernel
Activate the venv:
conda activate stat1010
start VSC by typing
code .
in the anaconda terminal.open/create a
.ipynb
or.py
file.Select the kernel
stat1010
Run a code cell by pressing
Shift+Enter
or click the triangular play button.Continue to run other cells.
After finishing using VSC, close the VSC, and deactivate the virtual environment in a conda terminal:
conda deactivate