Atharva Bhagwat

Stage 11: Setting up your computer

0Plan
Stage 11Level up
★★

Install Python tools on your own machine, once, so your work lasts and you can run bigger jobs.

In Part 1 you worked in Colab, where everything was ready. That is the best way to learn, but it has limits: sessions reset and your files disappear, big datasets do not fit, and you cannot rebuild exactly the same setup next year. Your own computer fixes all of these, and it needs a little setup, once.

Terminal first aid

The terminal is a window where you type commands instead of clicking. It only does what you tell it. Open it now: Terminal on macOS (press Cmd+Space, type Terminal), PowerShell on Windows (Start menu, type PowerShell). End every line with Enter.

A terminal is always standing in one folder, called the working directory. These commands cover most of what you will need:

CommandWhat it does
pwdPrint the folder you are standing in.
lsList what is in this folder.
cd folder_nameStep into a folder. cd .. steps back out.
mkdir new_folderMake a new folder.
python script.pyRun a Python file.
  • Press Tab after typing the first letters of a name and the terminal finishes it.
  • Press ↑ to bring back the last command. Press Ctrl+C to stop something that is running.
  • A command that prints nothing usually worked. An error message is read exactly like in Stage 2: start at the last line.

1. Install VS Code

Download it from code.visualstudio.com and run the installer. VS Code is an editor where you write scripts, run notebooks, browse files and use a terminal in one window.

2. Install Miniconda from the terminal

conda makes environments: separate boxes, each with its own Python and its own libraries. A project can use one version of pandas while another project uses a different one, and neither breaks the other. Miniconda is the small installer. On a Mac, first type uname -m: arm64 means Apple silicon, x86_64 means Intel.

macOS / Linux terminal
uname -m

# Apple silicon Mac (the answer was arm64):
URL=https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh
# Intel Mac (x86_64): use this line instead
# URL=https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.sh
# Linux: use this line instead
# URL=https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh

mkdir -p ~/miniconda3
curl -L $URL -o ~/miniconda3/miniconda.sh
bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
rm ~/miniconda3/miniconda.sh

~/miniconda3/bin/conda init bash zsh
Windows (PowerShell)
Invoke-WebRequest -Uri "https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe" -OutFile ".\miniconda.exe"
Start-Process -FilePath ".\miniconda.exe" -ArgumentList "/InstallationType=JustMe","/RegisterPython=0","/S","/D=$env:USERPROFILE\miniconda3" -Wait
Remove-Item ".\miniconda.exe"

& "$env:USERPROFILE\miniconda3\Scripts\conda.exe" init powershell

The last line connects conda to your terminal. Close the terminal completely and open a new one. The prompt should now start with (base). Check:

terminal
conda --version

3. Make an environment

Name it something short. We use pybio. This one command makes it and installs the libraries you used in Part 1:

terminal
conda create -n pybio -c conda-forge python=3.12 pandas numpy matplotlib seaborn scipy ipykernel -y

Switch into it with activate. Do this every time you open a new terminal. The prompt now starts with (pybio). If that prefix is missing, you are not inside the environment.

terminal
conda activate pybio
conda env list

conda env list shows every environment you have, with a star next to the active one.

4. See what is installed, and add more

pip is the tool that installs Python libraries. pip list shows everything in the active environment, with version numbers:

terminal
pip list

To add a library, use pip install and its name. For example, Biopython, which you will use in the optional DNA stage at the end:

terminal
pip install biopython
pip list

Run pip list again and biopython is now in the list. To remove a library: pip uninstall biopython.

5. Save and share an environment

Six months from now, or on a colleague's laptop, you will want exactly the same setup. There are two files for that.

environment.yml describes a whole conda environment. A YAML file is just a plain text file of settings written as name: value and lists with dashes. conda writes it for you:

terminal
conda env export --from-history > environment.yml

Open environment.yml in VS Code to read it. It names the environment, the places packages come from, and the packages you asked for. Anyone can rebuild your environment from it:

terminal
conda env create -f environment.yml

requirements.txt is the plainer cousin: just a list of Python libraries, one per line. pip writes and reads it:

terminal
pip freeze > requirements.txt
pip install -r requirements.txt
  • > means "put the output into this file".
  • -f means "from this file", and -r means "read this list".
  • Use environment.yml when others use conda, and requirements.txt when they only use pip. Many projects keep both.

6. Add VS Code extensions

An extension teaches VS Code new skills. You install them with a few clicks:

  1. Click the Extensions icon in the bar on the far left (four squares, one drifting away). Or use the menu View, then Extensions. Shortcut: Ctrl+Shift+X (Cmd+Shift+X on Mac).
  2. Click the search box at the top. It says Search Extensions in Marketplace.
  3. Type the name from the table below, for example Python.
  4. Check the publisher under the title and the download count (good ones have millions or hundreds of thousands).
  5. Click the blue Install button. When it changes to a gear icon, it worked. If you see a Reload button, click it.
  6. Repeat for each extension.
Search forPublisherWhat it does for you
PythonMicrosoftRuns .py files and finds your conda environments.
JupyterMicrosoftOpens notebooks (.ipynb) inside VS Code.
Rainbow CSVmechatronerColours the columns of a CSV so raw data is readable.
Error LensusernamehwShows errors at the end of the line that caused them.

To check, type @installed in the search box. All four should be listed.

7. Open a notebook in your environment

  1. File, then Open Folder, and choose a folder for your work.
  2. File, then New File, choose Jupyter Notebook (or open a notebook you downloaded from Colab with File, Download, .ipynb).
  3. Click Select Kernel at the top right, choose Python Environments, then pick pybio.
  4. Run a cell containing the two lines below.
run this in a notebook cell
import sys
print(sys.executable)

It prints the path of the Python being used. It should contain envs/pybio (or envs\pybio on Windows). If not, pick the kernel again.

Exercises

These are also in the notebook. Try each one yourself before you open the solution.

Terminal drill

Open your terminal and, using only the commands from the table, do this in order: (1) print where you are; (2) make a folder called practice; (3) step into it; (4) make two folders inside it called data and results; (5) list what is there (you should see both); (6) step back out and print where you are.
Show a solution (try it first)
one solution
pwd
mkdir practice
cd practice
mkdir data results
ls
cd ..
pwd

Make and use a second environment

Create an environment called test-env with Python in it, activate it, run pip list and notice how short the list is, then go back to pybio and delete test-env.
Show a solution (try it first)
one solution
conda create -n test-env python=3.12 -y
conda activate test-env
pip list
conda deactivate
conda activate pybio
conda remove -n test-env --all -y

Check your understanding

First star

Type conda env list in your terminal and paste everything it printed. The checker looks for your pybio environment.

Stuck? Get a nudge

If the list is missing it, the create command in step 3 did not finish. Run it again and read the last lines it prints.

Special thanks to Nilay!