{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "22ff25a7",
   "metadata": {},
   "source": [
    "# Python installation\n",
    "\n",
    "In this course, we will use **Python 3.12.2** because we want to utilize an autograder in Jupyter Notebook to provide you with instant feedback on your answers to some of the questions. It is very common for an OS, Windows or otherwise, to have multiple Python installations, so you may install different versions of Python on your computer. Python 3.13, for example, allows you to exit the Python shell using `exit` instead of `exit()`, which you may prefer when not working on py. \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c84f3643-a632-48fc-9957-7a95b6dd322d",
   "metadata": {},
   "source": [
    "## System Tools\n",
    "\n",
    "In order to navigate the Python data science ecosystem, you need to learn how to use the terminal (command line/console...). The following introduction should get you started with your learning.\n",
    "\n",
    "- **Terminal**: Terminals are programs that you open to access the `shell`. Common terminal programs include Windows PowerShell/Command Prompt, macOS Terminal, and Linux Terminal.\n",
    "- **Shell**: A shell interprets/executes commands and interfaces with the operating system. Common shells include PowerShell/Command Prompt in Windows, bash in macOS and Linux, and zsh in macOS. For example, if you issue the `ls` command in a shell (such as bash or PowerShell), the shell interprets the command, talks to the OS, and returns a list of files and directories.\n",
    "- **REPL**: A REPL (Read–Eval–Print Loop) is an interactive environment where you run code interactively, meaning that you type the code, the REPL interprets and shows results immediately using the language interpreter engine. For example, issuing the command `python` (or `python3` or `py`) in the terminal will start the Python REPL, where you write Python code to receive instant results. \n",
    "\n",
    "    For example, in Windows, you may start PowerShell (the terminal and the shell), and type `python` or `python3` to start the **Python REPL** (often referred to as **Python shell** or Python interpreter). You will see the <span style=\"color:white; background:darkred\">**>>>**</span> prompt, where you input code for immedate evaluation:  \n",
    "\n",
    "````{tab-set}\n",
    "\n",
    "```{tab-item} Windows\n",
    "```powershell\n",
    "PS C:\\Users\\[user]> python\n",
    "Python 3.12.2 (tags/v3.12.2:6abddd9, Feb  6 2024, 21:26:36) [MSC v.1937 64 bit (AMD64)] on win32\n",
    "Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n",
    ">>> 1 + 1\n",
    "2\n",
    ">>>\n",
    "```\n",
    "\n",
    "```{tab-item} macOS\n",
    "```bash\n",
    "[user]@mac:~$ python3.12\n",
    "Python 3.12.12 (main, Oct  9 2025, 11:07:00) [Clang 17.0.0 (clang-1700.3.19.1)] on darwin\n",
    "Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n",
    ">>> exit()\n",
    "[user]@mac:~$ python\n",
    "Python 3.13.7 (main, Aug 14 2025, 11:12:11) [Clang 17.0.0 (clang-1700.0.13.3)] on darwin\n",
    "Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n",
    ">>> 1 + 1\n",
    "2\n",
    ">>> \n",
    "```\n",
    "````"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "389fbc6d-89fa-4056-bec3-e068870c6a75",
   "metadata": {},
   "source": [
    "## Python Installation"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19c4c57e-ca86-426f-8b89-f8fcb31a39de",
   "metadata": {},
   "source": [
    "### Check Python Versions\n",
    "\n",
    "macOS and Linux distributions ship with Python (`python3`), and Windows usually does not include Python by default. However, since Python is widely used by other tools and packages, you more than likely have some versions of Python automatically installed in your system when installing other packages. Let us check which version(s) of Python are already installed on your computer.\n",
    "\n",
    "::::{tab-set}\n",
    ":::{tab-item} Windows\n",
    "You use the Windows Python Launcher (`py.exe`) in PowerShell and the `where python` command in the **Command Prompt (cmd.exe)** to view the current Python installation(s) (here, I have a version 3.12 installed already).\n",
    "\n",
    "With Windows PowerShell, `where python` does not give us anything, but the `py` launcher shows all installations and the default Python:\n",
    "\n",
    "```powershell\n",
    "PS C:\\Users\\[user]> where python\n",
    "PS C:\\Users\\[user]> py -0\n",
    " -V:3.13          Python 3.13 (64-bit)\n",
    " -V:3.13-arm64    Python 3.13 (Store)\n",
    " -V:3.12          Python 3.12 (64-bit)\n",
    " -V:3.10 *        Python 3.10 (64-bit)\n",
    "PS C:\\Users\\[user]>\n",
    "```\n",
    "\n",
    "With Command Prompt, the py launcher gives us the same information, and `where python` gives us the paths of the Python executables. \n",
    "\n",
    "```powershell\n",
    "C:\\Users\\[user]>py -0\n",
    " -V:3.13          Python 3.13 (64-bit)\n",
    " -V:3.13-arm64    Python 3.13 (Store)\n",
    " -V:3.12          Python 3.12 (64-bit)\n",
    " -V:3.10 *        Python 3.10 (64-bit)\n",
    "\n",
    "C:\\Users\\[user]>where python\n",
    "C:\\Users\\[user]\\AppData\\Local\\Programs\\Python\\Python312\\python.exe\n",
    "C:\\Users\\[user]\\AppData\\Local\\Programs\\Python\\Python313\\python.exe\n",
    "C:\\Users\\[user]\\AppData\\Local\\Programs\\Python\\Python310\\python.exe\n",
    "C:\\Users\\[user]\\AppData\\Local\\Microsoft\\WindowsApps\\python.exe\n",
    "\n",
    "C:\\Users\\[user]> \n",
    "```\n",
    ":::\n",
    "\n",
    ":::{tab-item} macOS\n",
    "If you are using macOS, you already have a version of Python shipped with the OS. You may issue the following commands in the command line (using `Terminal.app`) to see the version(s) of Python you have in the current system. In most cases, you may need to use `python3` instead of `python`. Additionally, `which python3` will display the path of the Python executable. \n",
    "\n",
    "```bash\n",
    "[user]@[host]:~/workspace/py$ python3 --version\n",
    "Python 3.12.2\n",
    "[user]@[host]:~/workspace/py$ which python3\n",
    "/Library/Frameworks/Python.framework/Versions/3.12/bin/python3\n",
    "[user]@[host]:~/workspace/py$ \n",
    "```\n",
    "\n",
    "Or, to be specific, to see the Python version(s) you install from python.org: \n",
    "```bash\n",
    "[user]@mac:~$ ls /Library/Frameworks/Python.framework/Versions/\n",
    "3.12    Current\n",
    "```\n",
    "\n",
    "And, to see the Python installations through Homebrew (recommended): \n",
    "```bash\n",
    "[user]@mac:~$ brew list | grep python\n",
    "python@3.12\n",
    "python@3.13\n",
    "```\n",
    "\n",
    "Actually, all the Homebrew Python installations in this case are in `/opt/homebrew/Cellar/` with symbolic links in `/opt/homebrew/opt/`:\n",
    "```bash\n",
    "tychen✪mac:~$ ll /opt/homebrew/opt/python*\n",
    "lrwxr-xr-x  1 tychen  admin  28 Sep 10 10:48 /opt/homebrew/opt/python -> ../Cellar/python@3.13/3.13.7\n",
    "lrwxr-xr-x  1 tychen  admin  28 Sep 10 10:48 /opt/homebrew/opt/python@3 -> ../Cellar/python@3.13/3.13.7\n",
    "lrwxr-xr-x@ 1 tychen  admin  29 Oct 30 23:44 /opt/homebrew/opt/python@3.12 -> ../Cellar/python@3.12/3.12.12\n",
    "lrwxr-xr-x  1 tychen  admin  28 Sep 10 10:48 /opt/homebrew/opt/python@3.13 -> ../Cellar/python@3.13/3.13.7\n",
    "lrwxr-xr-x  1 tychen  admin  28 Sep 10 10:48 /opt/homebrew/opt/python3 -> ../Cellar/python@3.13/3.13.7\n",
    "```\n",
    "You can see that: \n",
    "- `/opt/homebrew/opt/python` is a link that points to `/opt/homebrew/Cellar/python@3.13/3.13.7`,\n",
    "- `/opt/homebrew/opt/python3` also points `/opt/homebrew/Cellar/python@3.13/3.13.7`, \n",
    "- `/opt/homebrew/opt/python@3` also points `/opt/homebrew/Cellar/python@3.13/3.13.7`, and\n",
    "- `/opt/homebrew/opt/pytholn@3.13` also point `/opt/homebrew/Cellar/python@3.13/3.13.7`. Only\n",
    "- `/opt/homebrew/opt/python@3.12` points to `../Cellar/python@3.13/3.13.7`.\n",
    "\n",
    "So, when you type `python` or `python3` in the terminal, you are pointing to the same `python3.13.7`. Only when you type `python3.12` are you specifying Python 3.12:\n",
    "```bash\n",
    "tychen✪mac:/opt/homebrew/Cellar$ python\n",
    "Python 3.13.7 (main, Aug 14 2025, 11:12:11) [Clang 17.0.0 (clang-1700.0.13.3)] on darwin\n",
    "Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n",
    ">>> exit\n",
    "tychen✪mac:/opt/homebrew/Cellar$ python3\n",
    "Python 3.13.7 (main, Aug 14 2025, 11:12:11) [Clang 17.0.0 (clang-1700.0.13.3)] on darwin\n",
    "Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n",
    ">>> exit\n",
    "tychen✪mac:/opt/homebrew/Cellar$ python3.12\n",
    "Python 3.12.12 (main, Oct  9 2025, 11:07:00) [Clang 17.0.0 (clang-1700.3.19.1)] on darwin\n",
    "Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n",
    ">>> \n",
    "```\n",
    ":::\n",
    "::::"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "329b64c9",
   "metadata": {},
   "source": [
    "### Install Python\n",
    "\n",
    "If the check above does not yield any results, or if you don't see Python 3.12.2, go to [python.org](https://python.org) and download the installer for the specific version, then install it. If you are on macOS, after installation, check to see if the intended version is installed using `python2` or `python`. \n",
    "\n",
    "```bash\n",
    "$ python3\n",
    "Python 3.12.10 (main, May 22 2025, 21:57:53) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin\n",
    "Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n",
    ">>>\n",
    "```\n",
    "\n",
    "If you are a Windows user, the situation is somewhat more complex. When you search for Python from the Windows search bar, it defaults to directing you to the Windows Store for the current version of Python. However, the path of the Python installed will not be added to the `PATH` environment variable. It is therefore recommended to download the Python installer from python.org for installation. \n",
    "\n",
    "If you have already installed Python through the Windows Store, the installation will run automatically. You can check this installation using `where python` in Command Prompt, and you will see that a `python.exe` is in the `C:\\Users\\[user]\\AppData\\Local\\Microsoft\\WindowsApps\\` directory:\n",
    "\n",
    "```bash\n",
    "C:\\Users\\[user]> where python\n",
    "C:\\Users\\[user]\\AppData\\Local\\Programs\\Python\\Python312\\python.exe    ### python.org installer installation\n",
    "C:\\Users\\[user]\\AppData\\Local\\Microsoft\\WindowsApps\\python.exe        ### Windows Store installation\n",
    "```\n",
    "\n",
    "We can check the newly installed Python version by going into the location and running the Python executable to see the version. \n",
    "\n",
    "```bash\n",
    "cd \\Users\\[user]\\AppData\\Local\\Microsoft\\WindowsApps \n",
    "C:\\Users\\[user]\\AppData\\Local\\Microsoft\\WindowsApps>.\\python.exe\n",
    "Python 3.13.7 (tags/v3.13.7:bcee1c3, Aug 14 2025, 14:15:11) [MSC v.1944 64 bit (AMD64)] on win32\n",
    "Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n",
    ">>> \n",
    "```\n",
    "\n",
    "Since Python 3.12.2 is an earlier version, installing it by downloading the Python installer from [python.org](https://python.org) is preferred. Simply navigate to the download section of the site, search for the desired version, download the installer, and double-click it to initiate the installation. Make sure that you check the options below when installing:\n",
    "\n",
    "  - Use admin privileges when installing py.exe (this enables py.exe to manage all Python installations)\n",
    "  - Add python.exe to the PATH environment variable (this sets this installation as the default Python)\n",
    "\n",
    "```{figure} ../../images/python_windows_install.png\n",
    "---\n",
    "width: 400px\n",
    "name: python_windows_install\n",
    "---\n",
    "Python Installation Options in Windows\n",
    "```\n",
    "\n",
    "\n",
    "After completing the installations, you may check the Python installations you have using Windows PowerShell:\n",
    "```bash\n",
    "PS C:\\Users\\[user]> py -0\n",
    " -V:3.13 *        Python 3.13 (64-bit)    ### if you use py launcher this is the default Python\n",
    " -V:3.13-arm64    Python 3.13 (Store)\n",
    " -V:3.12          Python 3.12 (64-bit)\n",
    "PS C:\\Users\\[user]>\n",
    "```\n",
    "In Windows Command Prompt, you may see:\n",
    "```bash\n",
    "C:\\Users\\[user]>where python\n",
    "C:\\Users\\[user]\\AppData\\Local\\Programs\\Python\\Python313\\python.exe\n",
    "C:\\Users\\[user]\\AppData\\Local\\Programs\\Python\\Python312\\python.exe\n",
    "C:\\Users\\[user]\\AppData\\Local\\Microsoft\\WindowsApps\\python.exe\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "787ef975-0adc-4a5d-b414-26df67390eb4",
   "metadata": {},
   "source": [
    "### Choose Python Version\n",
    "\n",
    "If you have more than one Python version installed, you may need to specify the version of Python you use for your projects. If the default Python is different from the desired version, you may use the version **suffixes** to designate the desired Python version. This helps, e.g., when you need to create a virtual environment, and you need to designate a Python version such as 3.12.\n",
    "\n",
    "::::{tab-set}\n",
    ":::{tab-item} Windows\n",
    "In Windows, you may use the Python launcher to decide which Python version to use. This means, instead of issuing the `python` command, you use `py -Major.Minor` to control which Python version to use. For example, to use version 3.12, you issue:\n",
    "\n",
    "```bash\n",
    "C:\\Users\\[user]>py -3.12\n",
    "Python 3.12.2 (tags/v3.12.2:6abddd9, Feb  6 2024, 21:26:36) \n",
    "[MSC v.1937 64 bit (AMD64)] on win32\n",
    "Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n",
    ">>>\n",
    "```\n",
    ":::\n",
    "\n",
    ":::{tab-item} macOS\n",
    "In macOS, we append the Python version number in the format of `Major.Minor` when issuing `python` to choose the Python version to use:\n",
    "\n",
    "```bash\n",
    "$ python3.12\n",
    "Python 3.12.2 (v3.12.2:6abddd9f6a, Feb  6 2024, 17:02:06) \n",
    "[Clang 13.0.0 (clang-1300.0.29.30)] on darwin\n",
    "Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n",
    ">>> \n",
    "```\n",
    ":::\n",
    "::::"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b33e8667",
   "metadata": {},
   "source": [
    "### Set default Python version\n",
    "\n",
    "You can also change the system default Python version for the command `python` by modifying the path entries in the operating system's PATH environment variable (restarting the computer may be needed).\n",
    "\n",
    "::::{tab-set}\n",
    ":::{tab-item} Windows\n",
    "\n",
    "In Windows, you want to move the desired Python entry to the top of the PATH environment variable:\n",
    "\n",
    "1. Search for \"Edit the system environment variables\", and you will see the \"System Properties\" window. \n",
    "2. Click on the \"Environment Variables\" button near the bottom of the window.\n",
    "4. In the Environment Variables window, click to highlight \"Path\" in the \"User variables for [USER]\" pane.\n",
    "5. Click the \"Edit\" button to bring up the \"Edit environment variables\" window.\n",
    "6. Using the Move Up button on the right, move the Python version entry (e.g., Python312) to the top of the list. \n",
    "\n",
    "```{figure} ../../images/python_EV_order.png\n",
    "---\n",
    "width: 450px\n",
    "name: python_EV-order\n",
    "---\n",
    "The top Python path is the default Python\n",
    "```\n",
    "\n",
    "This will point the default `python` to your desired version; in this case, Python 3.12:\n",
    "\n",
    "```bash\n",
    "PS \\\\Users\\[user]> python\n",
    "Python 3.12.2 (tags/v3.12.2:6abddd9, Feb  6 2024, 21:26:36) \n",
    "[MSC v.1937 64 bit (AMD64)] on win32\n",
    "Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n",
    ">>>\n",
    "```\n",
    ":::\n",
    "\n",
    ":::{tab-item} macOS\n",
    "If you are using macOS, you may change the PATH environment variable by following these steps: \n",
    "\n",
    "1. Check Python installation and the default Python version. The example below shows that Linux database does not recognize `python` but `python3` and `python3.12` are recognized by the system. \n",
    "```bash\n",
    "  [user]@mac:~$ which python\n",
    "  [user]@mac:~$ which python3\n",
    "  /opt/homebrew/bin/python3\n",
    "  [user]@mac:~$ which python3.12\n",
    "  /opt/homebrew/bin/python3.12\n",
    "  [user]@mac:~$ \n",
    "```\n",
    "2. Edit shell configuration (~/.bashrc or ~/.zshrc)\n",
    "```bash\n",
    "  nano ~/.zshrc       ### if using zsh\n",
    "  nano ~/.bashrc      ### if using bash\n",
    "```\n",
    "3. Add preferred Python version to PATH\n",
    "```bash\n",
    "  export PATH=\"/usr/local/bin/python3.12:$PATH\"\n",
    "```\n",
    "\n",
    "or, if installed via Homebrew:\n",
    "\n",
    "```bash\n",
    "  export PATH=\"/opt/homebrew/bin:$PATH\"\n",
    "```\n",
    "4. Apply changes\n",
    "```bash\n",
    "  source ~/.zshrc       ### if using zsh\n",
    "  source ~/.bashrc      ### if using bash\n",
    "```\n",
    "\n",
    "Or, you may simply set an alias in one of your profile files (such as ~/.bashrc) by adding the line:\n",
    "```bash\n",
    "alias python='python3.12'\n",
    "```\n",
    ":::\n",
    "::::"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1433ac41-78ad-47d4-8600-5a0d8581e83e",
   "metadata": {},
   "source": [
    "## Resources\n",
    "\n",
    "- [How to Install Python on Your System: A Guide](https://realpython.com/installing-python/#windows-how-to-check-or-get-python) from realpython.com\n",
    "- [How to Install Python on Windows 10](https://www.digitalocean.com/community/tutorials/install-python-windows-10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "26f05abb-8333-46cf-8ff4-99fe7d861118",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
