Stage 11: Setting up your computer
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:
| Command | What it does |
|---|---|
pwd | Print the folder you are standing in. |
ls | List what is in this folder. |
cd folder_name | Step into a folder. cd .. steps back out. |
mkdir new_folder | Make a new folder. |
python script.py | Run 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.
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 zshInvoke-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 powershellThe last line connects conda to your terminal. Close the terminal completely and open a new one. The prompt should now start with (base). Check:
conda --version3. Make an environment
Name it something short. We use pybio. This one command makes it and installs the libraries you used in Part 1:
conda create -n pybio -c conda-forge python=3.12 pandas numpy matplotlib seaborn scipy ipykernel -ySwitch 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.
conda activate pybio
conda env listconda 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:
pip listTo add a library, use pip install and its name. For example, Biopython, which you will use in the optional DNA stage at the end:
pip install biopython
pip listRun 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:
conda env export --from-history > environment.ymlOpen 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:
conda env create -f environment.ymlrequirements.txt is the plainer cousin: just a list of Python libraries, one per line. pip writes and reads it:
pip freeze > requirements.txt
pip install -r requirements.txt>means "put the output into this file".-fmeans "from this file", and-rmeans "read this list".- Use
environment.ymlwhen others use conda, andrequirements.txtwhen 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:
- 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).
- Click the search box at the top. It says Search Extensions in Marketplace.
- Type the name from the table below, for example
Python. - Check the publisher under the title and the download count (good ones have millions or hundreds of thousands).
- Click the blue Install button. When it changes to a gear icon, it worked. If you see a Reload button, click it.
- Repeat for each extension.
| Search for | Publisher | What it does for you |
|---|---|---|
| Python | Microsoft | Runs .py files and finds your conda environments. |
| Jupyter | Microsoft | Opens notebooks (.ipynb) inside VS Code. |
| Rainbow CSV | mechatroner | Colours the columns of a CSV so raw data is readable. |
| Error Lens | usernamehw | Shows 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
- File, then Open Folder, and choose a folder for your work.
- File, then New File, choose Jupyter Notebook (or open a notebook you downloaded from Colab with File, Download, .ipynb).
- Click Select Kernel at the top right, choose Python Environments, then pick
pybio. - Run a cell containing the two lines below.
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
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)
pwd
mkdir practice
cd practice
mkdir data results
ls
cd ..
pwdMake and use a second environment
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)
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 -yCheck 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.
Second star
Run import sys; print(sys.executable) in a notebook cell in VS Code, using the pybio kernel, and paste the path it printed.
Stuck? Get a nudge
Not seeing pybio in the kernel list? Click the refresh icon in the picker, or restart VS Code once. The environment needs ipykernel, which step 3 installed.
Special thanks to Nilay!