Installing Python on Mac OSX

You are not logged in.

Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.

1) Installing Python

Python is pre-installed on MacOS X, but it is often an older version. For this class, you will need Python 3.6 or newer (3.12 is strongly recommended).

First open a Terminal window (from the Applications/Utilities folder). Type python3 --version and see whether it says Python 3.12.x. If it does, you are all set! If not, follow the instructions below:

If you did not already have the right version Python installed, you will need to install it. You can get a Python installer here:

You can test the installation by running python3 --version and making sure that Python reports its version as 3.12.x.

2) Text Editor or IDE

You will also need a program for editing your Python source files. If you have prior programming experience, you may or may not already have a preferred text editor (or Integrated Development Environment) for working with Python code. If you already have something you are familiar with, feel free to use that! If not, we recommend using the "IDLE" program (which is included with most Python installations) for editing your files.

3) Additional Packages

3.1) Install Xcode Tools

In order to install Numpy, Scipy, Matplotlib, and Jupyter Notebook you will first need to install the Mac OSX command line developer tools. Do this by entering the following command (without the dollar sign) at the terminal:

$ sudo xcode-select --install

If you are greeted with a window, you should click the "Install" button (you will not need to click "Get Xcode").

3.2) Install Numpy, Scipy, and Matplotlib

Next, install the extra packages by running the following command:

$ sudo pip3 install numpy scipy matplotlib jupyter pandas requests

This may take a while, so be patient. While you're waiting, you can read a little about how we just installed these packages in the section below--the process will be useful if you'd like to install other packages in the future.

3.3) What is pip?

The easiest, most cross-platform way to install a package like scipy or numpy is using pip, a Python package manager. Most Python installs should come with pip already, so you will likely have it, but if not, here is the documentation on pip installation.

Once you have pip installed, the next step is to open up some sort of shell. On a Mac, this would be Terminal, on Windows, this is usually Command Prompt, and on Linux, it varies based on your setup and distribution.

Once you have a terminal open, type pip install package_name (replacing package_name with scipy, numpy, or the name of your package). You may need to use administrative privileges for this. Once that's completed successfully, you can check to make sure the installation worked by opening up any python shell (for example, by typing python3 into the same terminal you used for the earlier step), and then trying import package_name. If there is no error output, it should be correctly installed!

If you are having trouble installing something, feel free to ask.

4) Testing Python installation

Now that you've finished installing everything, let's double check to make sure everything is working properly.

  • First, download the following zip file: test_python.zip and extract the contents.

  • Open the test_install.py file in IDLE (note the .py extension means the file is a Python program).

  • Run the code by clicking Run > Run Module at the top, which will first import all the packages you just installed and then display a message to the screen. If you see the output below after running the program this means that you have all the necessary packages successfully installed:

Note how it says 3.12.4 in the top left corner? That is the Python version that executed the program. If you have multiple versions of Python installed, you'll have to be careful about which version of Python is running, as older versions of Python2 use different commands, and older versions of Python3 may not have the proper packages installed / not have the latest functionality.

5) Testing Jupyter Notebook

A Jupyter notebook is a type of document that can contain both code and rich text elements (such as images, equations, and other formatted text). We will be using Jupyter notebooks throughout the course in tutorials and at least one assignment. Python notebooks such as Jupyter Notebook or a similar tool called Google Colab are often used in other courses and throughout research and industry for sharing results of an analysis program accompanied with non-code descriptions or visuals.

In this section we're going to open the test_notebook.ipynb file in Jupyter Notebook that was included in the test_python.zip file, create a new Jupyter Notebook, and show you some basic functionality of Jupyter notebooks. There is extensive documentation available here if you'd like to explore further, and we're also happy to answer questions!

5.1) Basics

Open a Terminal or Command Prompt and run the command jupyter notebook. In addition to the output below in the Terminal, this should also automatically open a page in your web browser. (Note that the page is not relying on an internet connection, perhaps unlike most pages you view with your browser. Rather, as is the case for all URLs beginning with localhost, the page is getting its content from your own "local" machine.)

The web page will look like the dashboard below. Open it now and follow along!

Notice the categories at the top: "Files," "Running," and "Clusters." You will likely live almost exclusively in the "Files" category, which lists all files and folders in the directory in which you ran jupyter notebook. (There may be non-Jupyter-Notebook files listed here, too.)

On the Files dashboard, navigate to and open the test_python folder and click on the test_notebook.ipynb notebook to open it, view it, and edit it. Once you open the test_notebook, you should see the following web page appear:

5.2) Creating New Jupyter Notebooks

From the Files dashboard, you can also make a new file. Click the "New" dropdown in the top right. Choose to create a new Python notebook1. It will open a blank notebook in a new tab:

You can click on "Untitled" at the top to change the name of the file.

The new file contains a single cell. Cells are components of Jupyter notebooks. Each cell can contain content of a particular type. For our purposes, there are two types of cells: code, or Markdown. The first cell is by default a code cell. Try typing in a Python expression there. Then, click the "Run" button (this one: ) to execute its contents. You should see the Python result:

After running the cell, Jupyter automatically gave us a new cell. We could have added a new cell ourselves using the "Add" () button. And we can delete cells with the "Remove" () button.

Let's make this new cell a Markdown cell. In the top menu bar, find the Dropdown that says "Code," and switch it to "Markdown." Markdown is a plain text formatting syntax. In other words, it's a set of rules about how to make text be displayed a certain way. If you've ever written _underscores_ around a word to italicize it, or **asterisks** around a word to make it bold, you've used Markdown! Here's a useful cheat sheet for the Markdown syntax, and there are examples below.

Type some things into your Markdown cell. When you run the cell, you'll see the results of the formatting on your text. For example, this cell:

becomes this, after running:

Markdown also supports inclusion of images. In order to include the Python picture in our first example, we moved the python.png file to the same folder as our Jupyter notebook, and included a Markdown cell with the following contents.

To quit Jupyter, close all the browser tabs and press "Ctrl-C" in the Terminal/Command Prompt in which you launched it.

5.3) Other Notes

The above should be all you need to know to about Jupyter notebooks for your other courses. Here are some other miscellaneous notes which you may find useful:

  • You can share variables and imports across cells. This makes it easy to separate your code into chunks, without having to reinitialize variables or reimport modules.
  • The keyboard shortcut for running a single cell while you have it selected is "Shift+Enter"
  • To run all the cells, you can choose "Run All" from the "Cell" dropdown menu.
  • As noted, you can generally stay almost exclusively in the "Files" category of your dashboard. If, however, you find that code in your notebooks takes quite long to run, you may wish to "Shutdown" some of the running notebooks in the "Running" category.

 
Footnotes

1"Jupyter" loosely stands for "Julia", "Python", and "R," the three programming languages for which the notebooks were originally designed. Jupyter supports over 40 other languages now! (click to return to text)