Python Installation
I. Why Python?
1/ What is it
- a beginner-friendly language with a really clean syntax, and it's everywhere: data, AI/ML, automation, web,...
- pip comes with it, it's the tool to install libraries (numpy, pandas,...)
- most of the Documents section (python tutorials, data analysis) runs on it
2/ python3, not python
- on mac u type
python3andpip3(plainpythonusually doesn't exist) - ur mac might already have a
python3(from the Command Line Tools). that one is old and meant for apple's own stuff, install ur own
II. Installing Python
1/ Download
- go to: https://www.python.org/downloads/macos/
- click on Download macOS 64-bit universal2 installer of the latest Python 3.x.x (a
.pkgfile)
2/ Run the installer
- open the
.pkgfile - Continue -> Agree -> Install
- enter ur mac password (or touch id) and wait
3/ Install Certificates (MACOS)
- after installing, a Finder window with the Python folder pops up (Applications > Python 3.x)
- double click
Install Certificates.command. a terminal window runs and installs the SSL certificates - without it, python code that downloads stuff (
urllib, web scraping, some datasets) can fail withCERTIFICATE_VERIFY_FAILED - close the window when it says done
4/ Check python3 & pip3
- open Terminal (Cmd + Space -> type
Terminal-> Enter) - paste these in and press Enter:
python3 --version
pip3 --version
which python3
- if the output is, for example,
Python 3.x.xandpip 2x.x ...then ure done which python3should show something like/usr/local/bin/python3or/Library/Frameworks/Python.framework/...(the one u installed). if it shows/usr/bin/python3, that's apple's one -> close and reopen the terminal
III. Python in VS Code
1/ Python extension
- Visual Studio Code -> Cmd + Shift + X (Extensions)
- search for
Pythonand install Python (Microsoft, the one with millions of downloads). it comes with Pylance (autocomplete, error hints)
2/ Select the interpreter
- Cmd + Shift + P -> type
Python: Select Interpreter-> choose the version u installed (not/usr/bin/python3) - u can see / change it later at the bottom right corner of VS Code
3/ First script
- create a file
hello.py:
hello.py
name = "mathalgo"
print(f"hello from {name}")
- run it with the ▷ on the top right, or in the terminal (Ctrl + `):
python3 hello.py
IV. pip & virtual environments
1/ Installing libraries
pip3 install numpy
pip3 list
2/ venv (one environment per project)
- each project gets its own libraries -> no version fights between projects
- inside ur project folder:
python3 -m venv .venv
source .venv/bin/activate
- now the terminal starts with
(.venv). inside it,pythonandpip(without the 3) work too. typedeactivateto get out - in VS Code, pick
.venvas the interpreter (step III.2)
V. Common errors
zsh: command not found: python: usepython3(or work inside a venv, wherepythonworks)CERTIFICATE_VERIFY_FAILED: u skipped step II.3. open Applications > Python 3.x and double clickInstall Certificates.commanderror: externally-managed-environmentwhen runningpip3 install: happens with a python installed by Homebrew. use a venv (IV.2) instead of installing globally- want Homebrew instead of the installer?
brew install pythonworks too, just remember the venv thing above
So overall: install from python.org, run Install Certificates.command, use python3 / pip3, one venv per project
VI. Reference Sources
- Python Docs. Using Python on macOS. docs.python.org
- Visual Studio Code Docs. Python in Visual Studio Code. code.visualstudio.com