# Python Set Up

March 07, 2018

This page explains how to set up Python on your computer so you can run and edit Python programs. We’ll be getting Python set-up on your computer. This will also include getting you familiar with the Terminal (which will let us run Python code), and setting up a text editor for writing code.

# Running Python interpreter from the Terminal

Let’s start by getting familiar with the Terminal. For Mac OS, this application is already installed and called Terminal. On Windows, it is called PowerShell. On Linux, it might be called Terminal, GNOME Terminal, Konsole or xterm.

Once you launch the terminal, we’ll try to start Python (most computers have Python installed by default!). To do this, type python into the Terminal, and press ENTER. You will see output similar to the following:

| ~ @ 17:02:03 => python
Python 2.7.11 (default, Jan 22 2016, 08:29:18)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
>>> 

If the above worked, then Python is already installed on your computer. You can skip the next section. If not, then you need to install Python. Continue reading.

# Installing Python (if required)

## Installing Python on Mac OS X or Linux

If you are on Mac OS X or Linux (this is rarely the case, since they come with Python pre-installed), follow the normal process for installing a package once the download is complete.

## Installing Python on Windows

Run the Python installer and accept all the defaults. Once done, confirm that the installation is complete by typing python into your Terminal (PowerShell). This should produce output similar to the one shown below (same as previous section).

| ~ @ 17:02:03 => python
Python 2.7.11 (default, Jan 22 2016, 08:29:18)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
>>> 

If Python is still not recognized, enter the following into your PowerShell.

[ENVIRONMENT]::SETENVIRONMENTVARIABLE("PATH", "\$ENV:PATH;C:\PYTHON27", "USER")

Again, once done, confirm that the installation is complete by typing python into your Terminal. If this works, Python is installed. Otherwise, see Python Windows FAQ for help.

# Write some Python!

If you’ve followed along, you should now have the Python interpreter open in front of you. Type print "Hello World!" and press ENTER. You should see a response in the next line, Hello World!, like so:

| ~ @ 17:02:03 => python
Python 2.7.11 (default, Jan 22 2016, 08:29:18)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
>>> print "Hello World!"
Hello World!
>>> # you can type expressions here ..

You can type Python code where it shows >>> . Congratulations on running your first line of Python!

To exit the Python interactive interpreter, use Ctrl-Z on Windows, and Ctrl-D on Mac OS X or Linux. You can also type quit() and press ENTER.

# Getting familiar with the Terminal

Now, we’ll learn how to create a directory (i.e. a folder) from the Terminal, how to go from one directory to another, and how to list all the files in a directory.

Once you have quit the Python interpreter, type mkdir my_code_dir to create a directory (mkdir stands for make directory). This will create a directory called my_code_dir. You can use the ls command to list all the files in the directory. And the cd {dir_name} command, to navigate to a particular directory. For example, you can do cd my_code_dir.

Here’s the sample interaction from my computer.

| ~ @ 17:48:40 => mkdir my_code_dir     # create "my_code_dir"
| ~ @ 17:53:28 => ls                    # list all folders and files
Applications
Desktop
Documents
Dropbox
Library
Movies
Music
Pictures
Public
my_code_dir
HarryPotter.avi
| ~ @ 17:53:28 => cd my_code_dir/      # change current directory
| ~/my_code_dir @ 17:55:30 => cd ../   # go back to parent directory
| ~ @ 17:55:39 =>

# Setting-up the Text Editor

Now that we can run Python interactively and are somewhat familiar with the Terminal, it’s time to install a text editor to write code in. (A Python program is just a text file that you edit directly).

You want a text editor with a little understanding of code and indentation. There are many good free ones. I recommend using Atom, but you can also use JEdit. If you are on Windows, do not use Notepad or Wordpad.

Once you’ve installed the text editor, create a new file and save it in my_code_dir as hello.py. Then type the following line into the file, print "Hello World!". Things should look something similar to the following:

# Running Python programs from the Terminal

The above is a simple 1-line Python program. Now, we’ll run the program from the terminal. To do so, first navigate to the code directory, then run python hello.py. Here’s the sample interaction from my computer:

| ~ @ 17:55:39 => cd my_code_dir/
| ~/my_code_dir @ 18:12:38 => python hello.py
Hello World! 

When we run the file, the Python program outputs Hello World!.

# Final words: Overall set-up

You should have the Terminal open where you can type python hello.py to run whatever program you are working on. At the Terminal prompt, just hit the up-arrow key to recall previously typed commands, so it’s easy to run previous commands without retyping them. You want an edit / run workflow that allows you to switch between editing and running easily.

The interpreter is great for little experiments. However, do most of your work in Python files. Since being able to write Python programs is the ultimate goal, it’s best to be in that mode the whole time and use the interpreter just for little experiments.

Lastly, it’s recommended that you add shortcuts for Terminal to and Atom (or whichever text editor you’re using) to your Desktop / Dock so that it’s easy for you to get to them as you go through this course.

# Appendix: Editor Settings

Following are some recommended settings for your text editor:

• When you press Tab, it’s best if your editor inserts spaces instead of a real tab character. All of the tutorial files use 2-spaces as the indent, and 4-spaces is another popular choice.
• It’s helpful if the editor will “auto indent” so when you press Enter, the new line starts with the same indentation as the previous line.
• When you save your files, use the unix line-ending convention, since that’s how the various starter files are set up. If running hello.py gives the error “Unknown option: -”, the file may have the wrong line-ending.

Here are the preferences to set for common editors to treat tabs and line-endings correctly for Python:

• Atom — Nothing to do! You’re all set!
• Windows Notepad++ — Tabs: Settings > Preferences > Edit Components > Tab settings, and Settings > Preferences > MISC for auto-indent. Line endings: Format > Convert, set to Unix.
• JEdit (any OS) — Line endings: Little ‘U’ ‘W’ ‘M’ on status bar, set it to ‘U’ (for Unix line-endings).
• Unix emacs — Tabs: manually set tabs-inserts-spaces mode: M-x set-variable(return) indent-tabs-mode(return) nil.