. _starting_up_python:

2.4.2. Starting up python

This section assumes you have installed Python.

The first thing some of you will want to do is to point and click at something to start Python.

Try not to do this at first. The problem is that the programs that installed Python may have done something to help you start up Python, but often the particulars of how that works are hard to get at. We would like to have as much control with how Python is run as possible. And we want to be able to know what window to look in to see the things a Python program prints out. This helps enormously in knowing what to do when something goes wrong.

Therefore, we are going to start Python from the commandline.

The following notes basically give you three ways to start up Python:

  1. Giving the python command at a command window.
  2. Using IPython
  3. Using IPython notebook

Version 3 is the recommended version for the first part of the course, but the first time you run Python you should try all three versions in the order given above to verify that

  1. Python is installed correctly.
  2. IPython is installed correctly.
  3. Your browser works with IPython notebook.

Ultimately, you will find all 3 ways of starting up Python useful, since they provide a way of interacting with Python that can be useful.

2.4.2.1. Commandline startup: Version I

From the commandline means you need to start a Terminal or an MSDos command window that allows you to type commands directly into it. In MacOS X, the default terminal program is called Terminal (From the Go Menu, select Utilities > Terminal); In Windows, it is is called MSDos (From the Start Menu, select Programs > MSDos Prompt).

Windows terminal

Windows terminal

MAcOSX terminal

MacOSX Terminal

For Windows 8 users, the following may help (from ComputerHope.com):

  1. From the Start screen type cmd and press enter; OR
  2. Move the mouse cursor to the very bottom-left corner of the screen and right-click or press Windows key + X. This will open the power user task menu, select either Command Prompt or Command Prompt (Admin).

2.4.2.2. The Python command: Windows

On Windows machines, the Python installation is usually placed in C:\Python27, though you can change this when you’re running the installer.

If python is installed in one of the usual places Windows looks for programs, the following command (issued in an MSDOS or Command window):

C:\Windows> python

will start up Python, which should look something like this:

Python 2.7 (#1, Feb 28 2010, 00:02:06)
Type "help", "copyright", "credits" or "license" for more information.
>>>

Try typing something at the “>>>” prompt:

>>> print 'hello'
hello

Python responds by printing ‘hello’, with no quotes. If the command is unknown, see th next section.

The program you are interacting with when you type commands to the “>>>” prompt is called a python shell. The interactive capability of Python is one of its most important features as a programming language, and one of the features that is most useful for beginners.

Throughout these notes, when indicating terminal commands (as opposed to commands to Python), we will assume a user named gawron whose home directory name (gawron) is printed out in the Terminal window as a prompt.

Typing an end-of-file character (Control-Z on Windows) at the primary prompt causes the shell to exit. If that doesn’t work, you can exit the interpreter by typing the following command: quit().

2.4.2.3. The Python command: MacOSX, Unix

With a normal Python install on MacOSX, you should just be able to type Python to your terminal window to start python.

That is:

~ gawron$ python

should start up Python, which looks like this:

Python 2.7.2 (default, Oct 11 2012, 20:14:37)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
 >>>

Try typing something at the “>>>” prompt:

>>> print 'hello'
hello

Python responds by printing ‘hello’, with no quotes.

The program you are interacting with when you type commands to the “>>>” prompt is called a python shell. The interactive capability of Python is one of its most important featurs as a programming language, and one of the features that is most useful for beginners.

Throughout these notes, when indicating terminal commands (as opposed to commands to Python), we will assume a user named gawron whose home directory name (gawron) is printed out in the Terminal window as a prompt.

Typing an end-of-file character (Control-D on Unix and MacOSX, Control-Z on Windows) at the primary prompt causes the Python shell program to exit. If that doesn’t work, you can exit the program by typing the following command: quit().

2.4.2.4. The Python command: Troubleshooting (Windows, MacOSX, Unix)

The instructions in this section should help you get python starting up in a command window, if that isn’t working on your machine. Following these instructions will also help you get IPython and jupyter notebook working in a command window.

If you installed the Enthought version of Python, starting up Python in a command window should look something like this:

fred:Introduction gawron$ python
Enthought Deployment Manager -- https://www.enthought.com
Python 2.7.13 |Enthought, Inc. (x86_64)| (default, Mar  2 2017, 08:20:50)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

The information printed out may differ in details such as the version number, but the first line should be the same; it should mention “Enthought.” If you installed Enthought Python and you get an error such as “python: command not found” or python starts, and you don’t see any information mentioning “Enthought” appearing, there are two possibilities:

  1. There is something wrong with the Enthought installation.
  2. Your machine is’t finding the right version of Python when you type python in a terminal window.

You can eliminate the first possibility by starting Canopy (which is installed as a separate program along with your enthought and starting Python from within Canopy). The Canopy startup window looks like this:

../_images/canopy_startup.png

Click on the Editor application and when the new window appears, select File > New > Python 2 File. You should get a split window. One of these windows is an editor window you can type Python commands into; the other is an Ipython window. You can run commands you’ve entered into the editor window by clicking on the green arrowhead in the menu bar. After running some code, there will be some output in the IPython window. For examples, if we type:

print True

in the editor window, and then click on the Green run arrow, the IPython window will look like this:

Welcome to Canopy's interactive data-analysis environment!
Type '?' for more information.
Python 2.7.13 |Enthought, Inc. (x86_64)| (default, Mar  2 2017, 08:20:50)
Type "copyright", "credits" or "license" for more information.

IPython 5.3.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

%run "/var/folders/j9/432gjp7j6v372x2bhj4cj_6m0000gp/T/tmpomsxPW.py"
True

Ok, so we are running Python. This means that the Enthought installation is working. If starting up Python via Canopy doesn’t work, you need to get back in touch with Enthought and find out what wrong with the installation process..

We turn here to investigating possibility 2, that the commandline window is not properly set up to find the Enthought installation of Python. Input the following commands into a Canopy editor window and run it:

import sys
print sys.path

This program prints the value of the name sys.path. The output in the Ipython window might look like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
%run "/var/folders/j9/432gjp7j6v372x2bhj4cj_6m0000gp/T/tmpqkhHTs.py"
['/Users/gawron/Library/Enthought/Canopy/edm/envs/User/lib/python27.zip',
 '/Users/gawron/Library/Enthought/Canopy/edm/envs/User/lib/python2.7',
 '/Users/gawron/Library/Enthought/Canopy/edm/envs/User/lib/python2.7/plat-darwin',
 '/Users/gawron/Library/Enthought/Canopy/edm/envs/User/lib/python2.7/plat-mac',
 '/Users/gawron/Library/Enthought/Canopy/edm/envs/User/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Users/gawron/Library/Enthought/Canopy/edm/envs/User/lib/python2.7/lib-tk',
 '/Users/gawron/Library/Enthought/Canopy/edm/envs/User/lib/python2.7/lib-old',
 '/Users/gawron/Library/Enthought/Canopy/edm/envs/User/lib/python2.7/lib-dynload',
 '/Users/gawron/Library/Enthought/Canopy/edm/envs/User/lib/python2.7/site-packages',
 '/Users/gawron/Library/Enthought/Canopy/edm/envs/User/lib/python2.7/site-packages/IPython/extensions',
 '/Users/gawron/.ipython']

sys.path is a list of the locations on your computer in which the Enthought installation looks for Python modules and programs. Look at the second entry in the list, a long path which ends with lib/python2.7. Call the part of this this long path that comes before lib/python2.7 $PYTHONHOME. In the example above, the value of $PYTHONHOME is:

/Users/gawron/Library/Enthought/Canopy/edm/envs/User/

Most of the other entries in sys.path are continuations of $PYTHONHOME. So the entries include:

$PYTHONHOME/lib/python2.7/plat-darwin
$PYTHONHOME/lib/python2.7/plat-mac
$PYTHONHOME/lib/python2.7/lib-tk
$PYTHONHOME/lib/python2.7/lib-dynload
$PYTHONHOME/lib/python2.7/site-packages

Some of these locations are specific to a Mac; others (such as site-packages) will show up in any Python.

Having determined the value of $PYTHONHOME, you know know the location of the Enthought python program. It is in $PYTHONHOME/bin. That is, you can now start up a command window, and start the right python by cutting and pasting the value of $PYTHONHOME into it, and following that location with “bin/python”. So for example, on my machine, with the above value for $PYTHONHOME, the complete command line is:

gawron$ /Users/gawron/Library/Enthought/Canopy/edm/envs/User/bin/python

If you hit return after entering this, you get the correct Python startup messages:

Enthought Deployment Manager -- https://www.enthought.com
Python 2.7.13 |Enthought, Inc. (x86_64)| (default, Mar  2 2017, 08:20:50)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Module readline not available.
>>>

So now we know how to start up the right version of python on your machine. Of course this is too much to type each time. What you need to do next is add the value of $PYTHONHOME/bin to your PATH, the list of places your command window looks when it starts up programs. The correct procedure for doing this varies from platform to platform.

Let’s start with most Mac and Unix based machines. There is a file executed every time you start a command window, which we can use to set the value of path. This is in your home directory (‘Users/gawron’ for me) and is called ”.bash_profile” (note the name starts with a ”.”). Note: every time you start a new command window, it is connected to your home directory.

What you need to do is edit that file with a plain text editor such as pico <https://www.ccsf.edu/Pub/Fac/pinepico.html>-, emacs, vi, or TextWranger. and add the following line:

export PATH=/Users/gawron/Library/Enthought/Canopy/edm/envs/User/bin:$PATH

This adds $PYTHONHOME/bin to $PATH and also preserves and existing values that were already there. If a file named ”.bash_profile” does not alredy exist in your home directory, that’s fine, create one, and insert just the above line. Under no circumstances should you use Microsoft Word to do your editing. Although Word has something called a “Save as Text” option, the designers were unable to resist the temptation to add idiosyncratic features that make the saved files unusable by other programs. The editors pico, vi and emacs all exist pre-installed on most Mac and Unix systems. You can run them by typing the command name followed by the file name in a command window; pico is by far the easiest to use, and should work fine for this task. So to edit ”.bash_profile” with pico, we would start up a fresh command window and type:

gawron$ pico .bash_profile

For Windows users, the concept of a path also exists, and the fix is the same. You have to find out what your $PYTHONHOME folder is (as done above) and add $PYTHONHOME/bin to the path. In changing the path, be sure that you only add a location; don’t delete any locations that are already there. For extra background look at Enthought’s commandline installation notes.

The ways to access the path have changed slightly in different versions of Windows.

../_images/Windows7_path.png

1n Windows 10 and Windows 8:

  1. In Search, search for and then select: System (Control Panel)
  2. Click the Advanced system settings link.
  3. Click Environment Variables. ...
  4. In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable.

2.4.2.5. Commandline startup: IPython

If you installed the Enthought version of Python, you got IPython along with it. And you can start up python with much more helpful set of tools available by typing:

~ gawron$ ipython

This starts up a Python shell with all the resources of a normal Python shell, plus some others. See the IPython website for a quick tour.

If you did not install the Enthought Python distribution, you can install IPython separately. by going to IPython.org.

The default directions will tell you to install IPython by installing Enthought. If you have reasons not to do this, you will have to follow the more complicated detaled instructions here.

Many of the scripts we will talk about involve displaying some sort of plot of your data. You will find that all those scripts run more smoothly if run through IPython with its pylab plotting interface set up. This is done by calling:

~ gawron$ ipython --pylab

when starting up IPython. What this does is set up an interface with a windows manager so that plots can be launched authomatically during the session. This is optional; the advantage is that when the same plot programs are run directly through a normal python shell, the interactive shell is frozen while the plot window is up, so that you can’t administer Python commands while continuing to look at the plot. Interaction can be restored by killing the plot window.

A routine IPython startup looks like this:

gawron$ ipython --pylab

Python 2.7.3 | 64-bit | (default, Jun 14 2013, 18:17:36)
Type "copyright", "credits" or "license" for more information.

IPython 1.0.dev -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

Welcome to pylab, a matplotlib-based Python environment [backend: MacOSX].
For more information, type 'help(pylab)'.
In [1]: print 'hello'
hello

Throughout these course materials we will use both standard python sessions and IPython sessions. Students are encouraged to use IPython whenever possible (or the canopy graphical-user interface which includes IPython), but are warned that some graph-plotting commands may not work quite the same way in standard python as they do in IPython. See Section Data Frames.

2.4.2.6. Commandline startup: IPython notebook

For the first part of the course, the recommended way to work on an assignment is to use Jupyter notebook.

Jupyter notebook is really just another way of interacting with python, just like IPython is, but Jupyter notebook uses your browser, has a very natural command interface, and makes an editable record of your Python session as you go (you will have to remember to save your session). There is plenty of excellent help available for using the notebook (with numerous examples) in the Jupyter notebook pages on the jupyter.org website.

You start IPython notebook as follows:

gawron$ jupyter notebook

If you want the pylab interface (for plotting graphs) you do:

gawron$ jupyter notebook  --pylab

The key idea is that this a combination editing environment and Python interpreter. First you break your programming task down into steps. For each step you do the following:

You can write some code, execute it, see the results, edit the code, and repeat until the results look right.

Then you can move on to on to another step. When you have completed all the steps in an assignment, you can save your results as a notebook, a special kind of document any one with IPython can run, and turn that notebook in.

Using the notebook gets you used to a write/execute/edit loop that scales right up to the way real data scientists work. Have a look at this blog post by a data scientist named Philip Guo describing how the Notebook changed the way he worked (written back when jupyter notebook was called ipython notebook).