Python Workflows

To test the Anaconda installation, a few exercises on Python workflow.

  • Scripts
  • Python interpreter
  • IPython interpreter
  • Jupyter notebook

Python scripts (".py" files)

Python scripts are one of the simplest ways to run python code - whatever's in the file just gets executed by the python interpreter as-is so it's very straightforward to see what's going on.

You can write scripts that run just like a normal program from the terminal. Open your favorite text editor and create a hello.py file containing the following line:

print('Hello World!')

Now from the bash terminal prompt run:

python hello.py

When you run the file this way you tell it to use the current active Python to run the file.

To make this script run like a command, add the following to the beginning of the file:

#!/usr/bin/env python

Then, make the script executable using the command:

$ chmod u+x hello.py

Now you will be able to run the script as:

$ ./hello.py

If a script needs to be run by a specific version of python, and not the one normally found when you type python on the command line, you can specify the full path to that python executable:

#!/Users/myusername/anaconda/envs/base-py2/bin/python

To learn more about this Unix magic, lookup shebang

Python and IPython interpreters

A more convenient way to work with Python is through a command line interpreter. This allows quick interactive work with Python, often useful for quick analysis or one-off activities.

The basic Python command line interpreter can be started by just typing:

python

This will create a prompt where you can issue python commands just like they were written in a script. For example, a simple print statement:

>>> print('Hello World!')
Hello World!

To quit the interpreter, just run exit() (or ctrl-D):

>>> exit()

This interpreter is pretty basic, no bells and whistles. A much more user friendly version is the interactive python interpreter or:

ipython

This is the interpreter I use for most of my work. Here, as above, you can do a simple print statement:

In [1]: print('Hello World!')
Hello World!

But you also have tab completion:

(In the below, by +Tab I mean press the tab key.)

>>> pri+Tab
>>> print

IPython includes more fancy features to make working easier. For example, p + up arrow will go through your history and only show you commands starting with p - try it and see!

Let’s set an integer variable, and then examine the documentation for an integer. Putting a ? after something in IPython and hitting enter will give you a info about that variable.

In [1]: a = 5
In [2]: a?

Doing<something>.+tab will list all of the “methods” of this objects or a bunch of thing Python inherently know about your variable (if you don't know what "methods" are, you will soon!)

In [1]: a = 5
In [2]: a.+tab

Jupyter notebooks

A final way of using python is through the Jupyter Notebooks (used for this right here!). Notebooks are convenient for science workflows because they allow full access to Python (or other programming languages) while storing a record of everything you've done, along with notes you might want to leave yourself, as well as multimedia content like plots or animations.

In your terminal fire up the notebook server:

jupyter notebook

This will pop up a web page. In the upper right corner click on the "New" tab and select the "Python3" option, which will start a notebook just like this one. Note that to actually make code run in a notebook, you have to use shift-enter (or ctrl-enter).

So we'll try out the exercises above in the notebook. In the notebook it's easy define functions, as shown below. (Be sure to hit shift-enter on that cell before trying to use it in the cell below!)

In [1]:
def hello(name='James'):
    print('Hello {}!'.format(name))
In [2]:
hello()
Hello James!
In [3]:
hello('Altez')
Hello Altez!
In [4]:
hello(name='')
Hello !