Friday, December 23, 2011

Computational Economics Lecture 3


Script Files

Until now we have just entered commands into interpreter
To write a longer program need to write a program file or script
  • Text file with Python commands that is run through the interpreter
  • Extension is 'py', as in 'filename.py'
We will look at a few ways of writing and running scripts
Note: The method of running scripts is very platform specific
  • This lecture is based on Python within Linux
    • But still worth a quick read if you're using Windows or Mac
  • If you are using Windows:
    • For Python(x,y), see the documentation or Google for help
    • These videos might be useful

Writing a Program

Normally, you will use a text editor like EmacsVimGeany, or Notepad++
Here is a demo with Geany (because it's simple)
Let's open Geany:


Now I write my script/program in Geany


And save it as test.py in my home directory, called /home/john
(Will look different on Windows/Mac)


Notice that Geany now knows its a Python file because of the .py extension
Geany applies syntax highlighting


We've now created a file containing Python commands, stored on the hard disk
The next step is to run it

Running Scripts from the Command Line

One method is to run it from the command line (or shell in UNIX/Linux)
  • Windows users can probably skip this section
Here I'm running it on Ubuntu Linux, through the shell


Better to use the -i flag to stay in Python when it finishes
  • So variables are not lost


Generally, the command line method is not the best one for scientific programming
Let's look at some other ways

Running Scripts with IDLE

A very easy way to run scripts is with IDLE
  • Fine to start, probably came together with your Python installation
    • If you can't find it, try a search of your hard disk
  • But not very high quality
    • Eventually you should learn to use IPython (see below)
After starting IDLE, the first window is the Python shell (interpreter)


Go to the file menu


Yes, I know the fonts are ugly...
Select "New Window" to get a window like this:


This window serves as a text editor where we can write our program:
  • Make sure you reproduce it exactly


Before running the file we need to save it
Select "Save As" from the "File" menu


You should get a dialogue box like this (Might look different on Windows/Mac)


Make sure you save it somewhere sensible, where you can find it again
Now that it's saved we can run it
Select "Run Module" from the "Run" menu
  • Or just press F5


The output is sent to the Python Shell


Running Scripts with IPython

After you get comfortable, switch to IPython
  • An enhanced interface to the Python interpreter
  • Requires text editor to write the file (plus IPython to run it)
  • Needs to be installed separately
Videos on installing and using IPython can be found here
Here's a very quick tutorial using screenshots from my Linux machine
First we fire up IPython


The next step is to run our file test.py in /home/john


The output is displayed in IPython
The only problem you might have is that test.py is not in IPython's current working directory
Error message looks like this


Let's check what directory we are in


'pwd' stands for "present working directory"
  • This is a UNIX command
  • Might be different according to your OS
  • Might need to prepend with % as in '%pwd'
    • Depends on whether 'automagic' is on or off
We are in /tmp, and the file is in /home/john
So I change to /home/john


'cd' stands for "change directory"
  • See comments for 'pwd'
Now it runs okay
Things will be different with Windows or Mac
  • I leave the details to you

0 comments: