Chuyển tới nội dung chính

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 python3 and pip3 (plain python usually 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​

2/ Run the installer​

  • open the .pkg file
  • 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 with CERTIFICATE_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.x and pip 2x.x ... then ure done
  • which python3 should show something like /usr/local/bin/python3 or /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 Python and 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, python and pip (without the 3) work too. type deactivate to get out
  • in VS Code, pick .venv as the interpreter (step III.2)

V. Common errors​

  • zsh: command not found: python: use python3 (or work inside a venv, where python works)
  • CERTIFICATE_VERIFY_FAILED: u skipped step II.3. open Applications > Python 3.x and double click Install Certificates.command
  • error: externally-managed-environment when running pip3 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 python works 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