2. Using the interpreter

 

 

 

 

 

 

 

Topics

 

 

¨  How Python runs programs

¨  How you run programs

¨  Example program launches

¨  Python configuration details

¨  A first look at module files

¨  The IDLE interface

¨  Other Python IDEs

 

 

 

 

 

 

How Python runs programs

 

 

 

·      Execution Model

 

Ø   A “script”: text file of statements, “mod.py”

 

Ø   Mod.py (sourcecode) =>

Mod.pyc (bytecode) =>

Python Virtual Machine

 

 

 

 

 

 

·      Execution Variations

 

Ø   Psyco: a just-in-time compiler for Python bytecode

 

Ø   Shedskin: Python-to-C++ compiler, if limited dynamic typing (avg 12X Psyco, 40x Python)

 

Ø   Frozen binary executables: Py2Exe (Windows), PyInstaller (+Linux), Freeze (Status?)

 

Ø   Other VMs: PyPy (Python VM in Python), Parrot (language neutral, pies)…

 

 

 

3 Implementations Today

 

Ø   C Python:      the standard

 

Ø   Jython:          for scripting Java apps

 

Ø   IronPython:   for scripting C#/.Net apps

 

Ø   Plus JPype, Python.net: other ways to Java/.Net

 

 

 

How you run programs

 

 

 

 

·      Program architecture

Ø   Program = multiple .py text files

o     One is the “main” top-level file: launch to run

o     Others are “modules”: libraries of tools

o     Some modules come from the “standard library”

Ø   Modules accessed and linked by imports: “import module”

o     Import = find it,  compile it (maybe),  run it (once)

Ø   Attributes fetched from objects: “module.attr”

o     Variables inside objects

 

 

 

 

 

 

 

 

b.py

def func():

    ......

 

a.py

import b

b.func()

 

 

sys.path (module import search path) =

“.” + PYTHONPATH + .pth files + Std Libs

 

 

3rd-party software often uses distutils “setup.py” script to install its code in auto-searched Lib\site-packages in Python install tree – no PYTHONPATH configuration required (see built-in tools unit for more on distutils)

 

 

 

 

·      Program launch options

Ø   Interactive coding  (“>>>”)

Ø   Shell/DOS command lines

Ø   Double-click icons  (raw_input trick)

Ø   IDLE: a free IDE  (Windows Start button, Runscript)

Ø   Module imports, reloads

Ø   Embedding calls

Ø   Unix executable scripts (#!)

Ø   Other IDEs: Komodo, PythonWare,…

 

 


 

Example program launches

 

 

 

¨  UNIX-style scripts

 

file: brian

#!/usr/local/bin/python            #or: /bin/env python

print 'The Bright Side of Life...' # another comment

 

% brian

 

 

¨  Command lines, module files

 

% python spam.py -i eggs -o bacon

 

 

¨  Interactive command line

 

% python

>>> print 'Hello world!'

Hello world!

>>> lumberjack = "okay"    # ctrl-D or ctrl-Z to exit

 

 

¨  Embedded code/objects (day 3)

 

Py_Initialize();

PyRun_SimpleString("x = brave + sir + robin");

 

 

 

¨  Platform-specific startup methods

·        Command-line interfaces

·        GUI start-up interfaces: .pyw files


 

 

 

Configuration details

 

 

 

 

¨   Module search path, for importing from other directories (only!)

Ø   PYTHONPATH shell/environment variable

Ø   “.pth” configuration files: C:\Python24\mypath.pth

Ø   sys.path builtin list changes , but temporary

Ø   Module search path =  [main file’s home directory + PYTHONPATH + standard libraries + .pth file contents]

Ø   Only add user-defined directories to search path (e.g. PYTHONPATH), not home directory or standard libs

 

¨   Other settings

Ø   Interactive startup file:       PYTHONSTARTUP

Ø   GUI variables (not on Windows): TCL_LIBRARY, TK_LIBRARY

Ø   System search path, to find python:  PATH

 

 

 

 

 

Installation details

 

 

¨   Python comes in binary or C source-code forms

¨   Windows self-installer: simple double-click, includes Tk

¨   Linux, Mac OS X: Python is a standard component

¨   C source code configures/builds automatically

¨   See Python source distribution “Readme” for install details

 

 


 

A UNIX config file (~/.cshrc or ~/.login)

 

 

#!/bin/csh

set path = (/usr/local/bin $path)

setenv PYTHONPATH /usr/home/pycode/utilities

setenv PYTHONPATH /usr/lib/pycode/package1:$PYTHONPATH

 

 

 

 

A Windows config file (C:\autoexec.bat)

 

·       Reboot after autoexec.bat changes

·       Use the ControlPanel/System/Advanced GUI to set this in recent versions of Windows (and restart Python)

·       “cd C:\Python24”, if you don’t want to set your PATH

 

doskey /insert

PATH %PATH%;C:\Python24

set PYTHONPATH=C:\pycode\utilities

set PYTHONPATH=D:\pycode\package1;%PYTHONPATH%

 

 

 

A path file (C:\Python24\mypath.pth)

 

 

c:\pycode\utilities

d:\pycode\package1

 

 

 

 

A GUI test session

 

¨   Type this to test your Python/GUI configuration

¨   On Linux, Mac OS X: may need extra packages (CD)

% python

>>> from Tkinter import *

>>> w = Button(text="Hello", command='exit')

>>> w.pack()

>>> w.mainloop()  # don’t type this line in IDLE!

 

 

 


 

 

 

 

Module files: a first look

 

 

¨   To avoid retyping code interactively

¨   ‘.py’ filename suffix needed if imported

¨   Import and use names at top-level of module file

¨   “dir(module)” lists a module’s attributes

¨   “help(module)” gives help

¨   “reload(module)” reruns an already-loaded file’s code again; import won’t!

 

 

 

 

file myfile.py

name = "The Meaning of Life"

 

 

% python                      ÜStart Python

>>> import myfile             ÜLoad module file

>>> print myfile.name         ÜUse its names

The Meaning of Life

 

 

% python                      ÜStart Python

>>> from myfile import name   ÜLoad names

>>> print name                ÜUse name directly

The Meaning of Life

 

 


 

 

The IDLE interface (new in 1.5.2)

 

 

¨   A simple integrated development environment

¨   Shipped and installed as part of the Python package

¨   Editor, syntax coloring, debugging, class browser, etc.

¨   Alternative to shell command line + editor, or clicking

¨   Written in Python, using the Tkinter GUI API

¨   See Python “Tools” directory, or Windows “Start” button entry

¨   Requires Python 1.5.2 or later, Tk 8.0 or later

¨   HINT: Alt-P/Alt-N scroll through command history

 

 

 

 

 

 

 

 

Other Python IDEs

 

 

 

 

·       IDLE: ships with Python

·       Eclipse, with PyDev (or other) Python plug-in

·       ActiveState: Komodo, VisualPython, PythonWin (http://www.activestate.com/)

·       PythonWare: Pythonworks (still active?) (http://www.pythonware.com/)

·       Others: WingIDE,… (http://www.python.org/ editors page)

·       Basic: text editor (Notepad, vi)  + command-line window (DOS, xterm)

 

GUI Builders (ahead)

 

·       Komodo and PythonWare include interactive GUI builders, that generate Tkinter code

·       Other GUI builders for wxPython, PyQt: BoaConstructor, BlackAdder, wxDesigner, QtDesigner

·       Application builders: Dabo, PythonCard

·       Website builders: Django, TurboGears, Zope, WebWare, mod_python, Plone, …

 

 


 

Time to start coding

 

 

 

¨   Lab exercises are at the end of your handbook

¨   Source code for lecture examples and lab solutions on disk

¨   Some solution write-ups appear after the labs section

¨   You are encouraged to "cheat": see solutions

¨   Ask the instructor for hints, tips, and help

 


 

 

Lab Session 1

 

Click here to go to lab exercises

Click here to go to exercise solutions

Click here to go to solution source files

Click here to go to lecture example files