Setting Up Python on Mac
Important: While Macs come with Python 2.7.x pre-installed, but because Python 2’s end-of-life is just around the corner, we’ll be working with Python 3 during this session so there’s some light installation ahead of us. This will require administrative privileges on your computer since the Python installation is usually done to protected directories. Along the way, we’ll also set up a more advanced text editor that will help us by providing some really specific programming features.
To install Python 3 for Mac:
- Download the package available here: Python 3.6.0 Installation Package for Mac.
- Double-click the downloaded file to start the installation process.
- Click through the wizard. The default settings are all fine to use. Close the wizard when complete. By default, the package installs into a central location which will require you to enter your system username and password during the process.
Let’s test that your installation went well by running the
python3 interpreter in the
Terminal. Practice doing this a few times until you are comfortable entering and exiting the Python shell.
- Open Terminal.
Enter the following into the terminal and press
You should then see something like this:
Python 3.6.0 (v3.6.0:41df79263a11, Dec 22 2016, 17:23:13)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
While inside the python interpreter:
>>>indicates that you are at a Python prompt.
- You can exit the Python prompt by typing
quit()and pressing enter or by using
While you can absolutely write Python code in any text editor, it is a lot easier to use one that is aware of code content and provides relevant features. To that end, we’ll be using Sublime Text 3. While there are a multitude of other options, Sublime 3 provides a good blend of simplicity and functionality.
Once downloaded, install it as you would any other OS X application. Being explicit, this means double-clicking the downloaded
dmg file, letting it open up, and subsequently dragging the
Sublime Text application into your
Be sure that you’ve set tabs equal to 4 spaces and the tabs to spaces setting is set to True.
- Go Sublime Text > Preferences > Settings.
Preferences.sublime - settings-Userfile, paste the following. This ensures that when you hit the Tab button on your keyboard, your text editor will do the equivalent of typing four spaces.
// The number of spaces a tab is considered equal to
// Set to true to insert spaces when tab is pressed
//... rest of file
Save the file.
The way your computer organizes storage is called a filesystem. This filesystem is organized like a tree with folders (also called
files. The highest point in the tree is the root directory located at
/. Everything you can access on your computer lives within the sub-directories of this root directory.
We often navigate the filesystem graphically (using Finder) where we can click and move deeper and across this directory tree. We can do that exact same navigation from the command line.
To do this, there are three commands that we’ll be using:
lslists the contents of a directory (“what’s in this folder?”).
pwdprints the full directory path to your current directory. It stands for “print working directory.” (“where am I?”)
cdmoves you into a new directory (it stands for “change directory”).
cd folder_name- Go into
cd ..- Go up one level in the folder heirarchy.
Let’s practice using these commands.
- Open up a Terminal (it’s under
Type each of these commands in order and hit
ls- This lists all the files in your home directory.
pwd- This displays the full directory path to your current directory, which is your home directory.
cd /- This will change you into the
ls- This lists the contents of the
cd Users- This will change you into the home subdirectory of the
ls- You should see a list of all the computer’s users’ home directories (and possibly a Shared folder).
pwd- This displays the full directory path to your current directory, which in this case is
..is a relative location meaning “parent directory”. You were in
/Users, so now you have moved up to your “parent”,
/, the root directory.
ls- This lists the contents of the root directory, confirming where you are.
cd ~- Just like how
..means “parent directory”,
~means “home” directory. When you execute this command, you’ll change back to your home directory, where all your user files are.
- You can use
tabto auto-complete directories, files, and programs. So from inside the root directory
/, if you type
cd Usand press the
tabkey, the command prompt will auto-complete the directory name, and you can then hit
enterto change into the
- The command prompt maintains a command history. You can use the up arrow to cycle through old commands. You can also see a list of your history by using the
- If you’d like to open a Finder window wherever you are in the Terminal, you can use the
open .command. Turns out
.is another “special place” that means “the current directory”. So this command opens the current directory, which OS X generally does with the help of Finder.
- If you’d like to change to a specific location in the Terminal, and you have a Finder window open already to that place, you can type in
cdto the Terminal, drag the little blue folder at the top of the Finder window into the Terminal, and then press
Answer these questions. Experiment at the command line if you need to! If you aren’t sure about an answer, ask a helper.
- What directory are you in after starting a new command line prompt?
- After starting a new command line prompt, how would you get to the root directory?
- How do you check what files and directories are in your current working directory?
- If you are in directory
/, and you want to get to
/Users/Shared/, how would you do that?
- What are 2 ways to avoid typing out a full navigation command? (hint: one requires that you’ve run the command before)
- What is the difference between a command prompt and a Python prompt?
You’ve practiced using
cd to navigate your computer’s filesystem using Terminal.
- You may want to just quickly go through the onboarding page.
- Go through this self-directed tutorial to start learning to read and write in Python. These concepts will be reviewed in the day two lesson, along with some more advanced topics.
Try some practice exercises. If you’ve been working on any other tutorials, feel free to go to those too, and ask an instructor to help anywhere you get stuck.
When you’re ready, let an instructor or assistant know. Together you will go through the following check off steps:
- Start a new command prompt, and from that command prompt start Python 3. Then quit Python 3.
Create a new Python file (with a
.pyextension). In that file, type the following and save the file. From a command prompt, navigate to this file and execute that Python script.
Open your text editor and press
Tab. Use the left arrow key to go back in that line and show that your text editor is using spaces to indent, not tabs.
- Test the Wordplay and ColorWall installations.
- Navigate to the Wordplay directory. Run the
words1.pyscript from your computer’s terminal. You should see a list of words that have two consecutive letter u’s.
- Navigate to the Colorwall directory. Run the
run.pyscript from your computer’s terminal. You should see a grid with color animations that lasts about a minute.
- Navigate to the Wordplay directory. Run the
- Show the instructor a practice exercise that seemed particularly challenging and how you worked it out.