PyOhio PyCamp 2014 :: The Compiler





Chris Calloway

University of North Carolina Renaissance Computing Institute

July 21-25, 2014

Copyright 2014 Chris Calloway - All Rights Reserved

The Compiler

Be sure you are in the pycamp folder on your desktop, and then:

> python
>>> import myscript
OK
>>>
  • To compile a script, use the import keyword to interpret the script once.
  • Notice: there is no .py at the end of myscript.
  • The interpreter generates Python bytecode from the script.
  • The interpreter saves the bytecode in myscript.pyc.
  • The interpreter then executes the compiled bytecode.

The Compiler

Python bytecode is not human-readable.

>>> ^D
> cat myscript.pyc # type myscript.pyc on Windows
Ñò
sÍ,Lc@s dGHdS(tOKN((((s
                       myscript.py<module>s
>

The Compiler

> python
>>> import myscript
OK
>>> import myscript
>>>
  • Each subsequent import of the compiled module skips interpretation.
  • Python keeps track of whether a module already is loaded into memory.
  • A module is simply a .py script which has been imported,
  • And hence has a corresponding .pyc bytecode file.

The Compiler

>>> ^D
> rm -f myscript.py # del myscript.py on Windows
> python myscript.pyc
OK
> python
>>> import myscript
OK
>>> ^D
>
  • The pre-compiled bytecode may be used in place of the script.
  • The .pyc version of the module is always imported if it already exists.

The Compiler

(1) > ls -l myscript.pyc # dir on Windows
    -rw-r--r--  1 cbc  tripython  110 Sep 14 16:00 myscript.pyc
    >

(1) Look.at the timestamp on myscript.pyc.

(The timestamp on your myscript.pyc will be different from the example above.)

The Compiler

(1) > ls -l myscript.pyc # dir on Windows
    -rw-r--r--  1 cbc  tripython  110 Sep 14 16:00 myscript.pyc
(2) > subl myscript.py # change print "OK" to print "OK Already"
    >

(2) Recreate and slightly change myscript.py in your favorite editor.

Have your new version of myscript.py to print "OK Already" instead of print "OK".

The Compiler

(1) > ls -l myscript.pyc # dir on Windows
    -rw-r--r--  1 cbc  tripython  110 Sep 14 16:00 myscript.pyc
(2) > subl myscript.py # change print "OK" to print "OK Already"
(3) > ls -l myscript.py
    -rw-r--r--  1 cbc  tripython  20  Sep 14 16:01 myscript.py
    >

(3) Look at the timestamp on your new myscript.py.

See that myscript.py has been modified more recently than myscript.pyc.

The Compiler

(1) > ls -l myscript.pyc # dir on Windows
    -rw-r--r--  1 cbc  tripython  110 Sep 14 16:00 myscript.pyc
(2) > subl myscript.py # change print "OK" to print "OK Already"
(3) > ls -l myscript.py
    -rw-r--r--  1 cbc  tripython  20  Sep 14 16:01 myscript.py
(4) > python
    >>> import myscript
    OK Already
    >>> ^D
    >

(4) import myscript again.

  • This will import your new myscript.py instead of the older myscript.pyc.

The Compiler

(1) > ls -l myscript.pyc # dir on Windows
    -rw-r--r--  1 cbc  tripython  110 Sep 14 16:00 myscript.pyc
(2) > subl myscript.py # change print "OK" to print "OK Already"
(3) > ls -l myscript.py
    -rw-r--r--  1 cbc  tripython  20  Sep 14 16:01 myscript.py
(4) > python
    >>> import myscript
    OK Already
    >>> ^D
(5) > ls -l myscript.pyc
    -rw-r--r--  1 cbc  tripython  118 Sep 14 16:02 myscript.pyc

(5) See that myscript.pyc has been updated!

The Compiler

(1) > ls -l myscript.pyc # dir on Windows
    -rw-r--r--  1 cbc  tripython  110 Sep 14 16:00 myscript.pyc
(2) > subl myscript.py # change print "OK" to print "OK Already"
(3) > ls -l myscript.py
    -rw-r--r--  1 cbc  tripython  20  Sep 14 16:01 myscript.py
(4) > python
    >>> import myscript
    OK Already
    >>> ^D
(5) > ls -l myscript.pyc
    -rw-r--r--  1 cbc  tripython  118 Sep 14 16:02 myscript.pyc
  • The .pyc is re-compiled if the .py version of the module has a more recently modified timestamp.
  • This keeps the .pyc file up to date with the .py file every time the script is imported as a module.

The Compiler

The Python interpreter consists of two parts:

  • A Python bytecode compiler, and
  • A virtual machine, which executes Python bytecode.

The Compiler

The Python interpreter consists of two parts:

  • The bytecode compiler accepts human-readable Python expressions and statements as input.
  • And produces machine-readable Python bytecode as output.

The Compiler

The Python interpreter consists of two parts:

  • The virtual machine accepts Python bytecode as input.
  • And executes the virtual machine instructions represented by the bytecode.

The Compiler

The bytecode in myscript.pyc represents these Python virtual machine instructions:

  1           0 LOAD_CONST               0 ('OK')
              3 PRINT_ITEM          
              4 PRINT_NEWLINE       
              5 LOAD_CONST               1 (None)
              8 RETURN_VALUE

The Compiler

The bytecode in myscript.pyc represents these Python virtual machine instructions:

  1           0 LOAD_CONST               0 ('OK')
              3 PRINT_ITEM          
              4 PRINT_NEWLINE       
              5 LOAD_CONST               1 (None)
              8 RETURN_VALUE
  • That's OK because the Python compiler takes care of converting Python language expressions and statements into bytecode for you.
  • You don't have to learn Python virtual machine instructions or the bytecodes which represents those instructions.

The Compiler

  • All you have to do to compile a Python script is import it as a module.
  • You normally do this anyway.
  • Python avoids the "edit-compile-run" development cycle.
  • Python is a rapid application development language.

The Compiler

  • Also, anything that may be done in a script, may also be done at the Python prompt.
  • You try things out first at the Python prompt.
  • Python avoids the "edit-compile-test-fix" development cycle.
  • Python is a prototyping language.

The Compiler

  • Two questions never to ask about Python:
  • Can Python do X?
  • What if I do Y in Python?
Just try it!