Python and VS Code Setup
Everything in the Python unit starts here. Do this once, then come back when something breaks.
Is Python Installed?
- Press
cmd + spaceto open Spotlight. - Type
IDLEand pressreturn. - IDLE opens: Python is installed. Click next to the
>>>prompt, typeprint("Hello, world!"), and pressreturn. You should seeHello, world!on the next line. - Nothing comes up: Python is not installed. Continue below.
Installing Python
- Press
cmd + space, search for Self Service, and open the CCSD Self Service app. - Search for Python and click Install. Wait for it to finish.
- Search Spotlight for
IDLEagain. It should open now.
Opening VS Code
- Press
cmd + space, typeVisual Studio Code, and pressreturn. - Go to File → Open Folder….
- Choose your Desktop, click New Folder, name it
python-class, and click Open.
Every Python file you write this unit goes in python-class. If VS Code asks whether you trust the authors of the folder, click Yes.
Writing and Running a Script
- File → New File, then File → Save as
hello.pyinsidepython-class. The.pyending matters. - Type:
print("Hello, world!")
print("Python is working.")- Save with
cmd + S. - Click the Run (play) button in the top-right corner of the editor. The output appears in the terminal panel at the bottom.
You can also run a script from the integrated terminal (Terminal → New Terminal, or ctrl + `):
python3 hello.pyBasic Terminal Commands
| Command | What it does | Example |
|---|---|---|
ls | List the files and folders where you are | ls |
pwd | Print the full path of where you are | pwd |
cd | Move into a folder | cd Desktop |
cd .. | Go up one folder | cd .. |
mkdir | Make a new folder | mkdir my-project |
touch | Make a new, empty file | touch notes.txt |
python3 file.py | Run a Python script | python3 hello.py |
Installing Libraries
A library is code someone else wrote that you can use. Install from the VS Code terminal, once per library:
python3 -m pip install pandas matplotlibThe terminal scrolls for a while. When the prompt comes back, it is done. If pip prints an error, raise your hand.
Troubleshooting
python3: command not found
Python is not installed. Install it from Self Service (above), then quit and reopen VS Code.
The Run button runs the wrong file The Run button runs whichever file is open in the editor. Click into the file you want first.
My file runs but nothing appears
A script with no print() runs silently. That is normal. Add a print().
SyntaxError
Python found a typo. The message gives a line number; go there and look for a missing colon, a mismatched quote, or a misspelled word.
FileNotFoundError when reading a CSV
The data file is not in the same folder as the script. Drag it into python-class and run again.
A plot window will not close, or windows keep piling up Close each plot window before running the script again.