Getting started with ipython and command-line basics

How to run Python interactively and as a shell script
Table of contents

Your system shell

The system shell is a text-only interface to your operating system. Whatever you could do via point-and-click, you can do with text commands. Here's a directory being created via the OS X shell:

However, there are many specific, precise, and powerful actions that can only be invoked through the system shell. We won't be learning very much of it. But we do need to know how to do these things:

  1. Get into the ipython shell
  2. Run the Python interpreter
  3. Move around our own file system
  4. Not get our system and ipython shells confused.

To access your system shell, which I'll often refer to as the command prompt, you'll have to run a program that opens a window to the shell. It's often referred to as the Terminal

Follow the instructions appropriate to your system:

Once you're at your Terminal/command-prompt window, you should see a nearly blank screen with perhaps a blinking cursor, waiting for you to type things in.

A few system shell commands

This is a live demonstration of a few system shell commands; read on for a little more elaboration.

Who am I?

If you're staring at a blank Terminal window and forgotten – or never actually knew – how you got there…then you can somehow forgotten where y


Where am I?

In OS X/Linux, the pwd command – short for "print working directory" – will print the full directory path that you're currently operating from.

In Windows, use: echo %cd%


Which version of Python am I running?

Whenever you run the python command from your system shell, you are calling for the Python interpreter to…interpret and execute some Python code. Most frequently, we usually pass in the name of a Python script file.

But we can invoke the python command with certain flags in special cases.

Right now, we just want to know which version of Python we're running:

python --version

Please install Anaconda

Note: if the result of your python --version is not something like:

Python 3.5.1 :: Anaconda 2.5.0 (x86_64)

– notably, the Python 3 and/or the Anaconda part, then you should probably install via the Anaconda Installer. Here are links to their one-click installs:

You can visit their installs page here:

Everything from this point on forwards assumes that you have

Entering (and exiting) ipython

While we can enter the standard interactive Python shell simply by executing python on its own…we want to use ipython…which comes with many, many conveniences.

Executing the ipython command is enough to get you to the Python shell. But before you run any Python-specific code, practice exiting the ipython shell by typing exit as soon as you get in:

You can also hit Ctrl-D to exit out of ipython. In fact, using your keyboard is the most highly recommended way.

How to know which shell you're in

It is very easy to get confused about the difference between your system shell and the ipython shell. They both technically do the same thing: interpret your text code into commands that get executed by your computer. You can write code that wipes out your file system via ipython or the system shell. The difference is that ipython interprets Python code. Your system shell interprets…whatever is specific to your operating system.

Ergo, running Python commands in your system shell will not work. And vice versa. Don't make the mistake that because both shells are text-based, or that you opened the ipython shell from the system shell, that they are the same thing. They are, in fact, completely different programs…just like when you click a hyperlink in a Word Document and it pop opens your web browser.

Here's an example of when you try to run the ipython command when you're already in the ipython shell:

Or, what happens when you exit when you've already exited from the ipython shell? Actually, you won't get an error because exit is a valid system shell command too. You'll just be booted out of your terminal:

Hello ipython

Now that we know how to get into the ipython shell, let's run some Python code.

Here's the classic Hello World program:

print("Hello World")

We can break it up into two lines:


Not printing to screen

The print() function does a fairly simple, but special job: it prints things to the screen.

To see why that's special, let's use a function that doesn't print to screen: the len() function – short for "length" – will return the value of whatever argument is passed to it:

len("Hello World")

In ipython, as with most interactive shells, you see the result of what the function returns immediately. Isn't that printing to screen? Yes, technically, but that's a convenient feature of the ipython shell. When we invoke the Python interpreter by itself – which we do later in this lesson – it's going to read Python code and not say a single thing to us…unless we tell it to via print() – or something breaks.

For now, see if you can notice the subtle difference between the print() and non-print() statements in ipython:



Running a Python script

Python scripts are just text files. They don't have to be named with a .py extension. And they don't need a special program to view or edit them – just any thing that can edit text.

Once you have a text editor installed and open, create a new file, and enter the following text:

print("Hello world!")
len("Hello world!")

Then save the file. Preferably somewhere like the Desktop – which is easily accessible via your system GUI and from the command line.

Give it a name like:

The following shell commands will:

cd ~/Desktop

Note how the output is just:

Hello world!

Were you expecting it to print the result of the len() function? Then you need to change that line in

print("Hello world!")
print(len("Hello world!"))

Are you in the right place?

The most common error I see, especially as the data-gathering gets complicated, are related to files not found. If you're getting this error, it isn't any simpler than this: you are not where you think you are:

All together

There's more to learn about the ipython shell and Python programming in general. But there's not much point in knowing Python if you can't communicate it to your computer. In this brief lesson, we've learned two ways to do it: