Program execution

Table of Contents

5.1. Executing code from a file
5.2. Interpreter and Compiler

5.1. Executing code from a file

Until now we have only worked interactively during an interpreter session. But each time we leave our session all definitions made are lost, and we have to re-enter them again in the next session of the interpreter whenever we need them. This is not very convenient. To avoid that, you can put your code in a file and then pass the file to the Python interpreter. Here is an example:

Example 5.1. Executing code from a file

Take the code for the cds translation as example and put it in a file named gc.py:


from string import *

cds = """atgagtgaacgtctgagcattaccccgctggggccgtatatcggcgcacaaa
tttcgggtgccgacctgacgcgcccgttaagcgataatcagtttgaacagctttaccatgcggtg
ctgcgccatcaggtggtgtttctacgcgatcaagctattacgccgcagcagcaacgcgcgctggc
ccagcgttttggcgaattgcatattcaccctgtttacccgcatgccgaaggggttgacgagatca
tcgtgctggatacccataacgataatccgccagataacgacaactggcataccgatgtgacattt
attgaaacgccacccgcaggggcgattctggcagctaaagagttaccttcgaccggcggtgatac
gctctggaccagcggtattgcggcctatgaggcgctctctgttcccttccgccagctgctgagtg
ggctgcgtgcggagcatgatttccgtaaatcgttcccggaatacaaataccgcaaaaccgaggag
gaacatcaacgctggcgcgaggcggtcgcgaaaaacccgccgttgctacatccggtggtgcgaac
gcatccggtgagcggtaaacaggcgctgtttgtgaatgaaggctttactacgcgaattgttgatg
tgagcgagaaagagagcgaagccttgttaagttttttgtttgcccatatcaccaaaccggagttt
caggtgcgctggcgctggcaaccaaatgatattgcgatttgggataaccgcgtgacccagcacta
tgccaatgccgattacctgccacagcgacggataatgcatcgggcgacgatccttggggataaac
cgttttatcgggcggggtaa""".replace("\n","")

gc = float(count(cds, 'g') + count(cds, 'c'))/ len(cds)

print gc
	
and now pass this file to the interpreter:
caroline:~/python_cours> python gc.py
0.54460093896713613
	

Tip

You can name your file as you like. However, there is a convention for files containing python code to have a py extension.

You can also make your file executable if you put the following line at the beginning of your file, indicating that this file has to be executed with the Python interpreter:

#! /usr/local/bin/python
      
(Don't forget to set the x execution bit under UNIX system.) Now you can execute your file:
caroline:~/python_cours> ./gc.py
0.54460093896713613
	
This will automatically call the Python interpreter and execute all the code in your file.

You can also load the code of a file in a interactive interpreter session with the -i option:

caroline:~/python_cours> python -i gc.py
0.54460093896713613
>>> 
This will start the interpreter, execute all the code in your file and than give you a Python prompt to continue:

>>> cds
'atgagtgaacgtctgagcattaccccgctggggccgtatatcggcgcacaaatttcgggtgccgacctgacgcgcccgttaagcgataatcagtttgaacagctttaccatgcggtgctgcgccatcaggtggtgtttctacgcgatcaagctattacgccgcagcagcaacgcgcgctggcccagcgttttggcgaattgcatattcaccctgtttacccgcatgccgaaggggttgacgagatcatcgtgctggatacccataacgataatccgccagataacgacaactggcataccgatgtgacatttattgaaacgccacccgcaggggcgattctggcagctaaagagttaccttcgaccggcggtgatacgctctggaccagcggtattgcggcctatgaggcgctctctgttcccttccgccagctgctgagtgggctgcgtgcggagcatgatttccgtaaatcgttcccggaatacaaataccgcaaaaccgaggaggaacatcaacgctggcgcgaggcggtcgcgaaaaacccgccgttgctacatccggtggtgcgaacgcatccggtgagcggtaaacaggcgctgtttgtgaatgaaggctttactacgcgaattgttgatgtgagcgagaaagagagcgaagccttgttaagttttttgtttgcccatatcaccaaaccggagtttcaggtgcgctggcgctggcaaccaaatgatattgcgatttgggataaccgcgtgacccagcactatgccaatgccgattacctgccacagcgacggataatgcatcgggcgacgatccttggggataaaccgttttatcgggcggggtaa'
>>>cds="atgagtgaacgtctgagcattaccccgctggggccgtatatcggcgcacaaatttcgggtgccgacctgacgcgcccgtt"
>>>cds
'atgagtgaacgtctgagcattaccccgctggggccgtatatcggcgcacaaatttcgggtgccgacctgacgcgcccgtt'
>>>gc
0.54460093896713613

Important

It is important to remember that the Python interpreter executes code from top to bottom, this is also true for code in a file. So, pay attention to define things before you use them.

Exercise 5.1. Execute code from a file

Take all expressions that we have written so far and put them in a file.

Important

Notice that you have to ask explicitly for printing a result when you execute some code from a file, while an interactive interpreter session the result of the execution of a statement is printed automatically. So to view the result of the translate function in the code above, the print statement is necessary in the file version, whereas during an interactive interpreter session we have never written it.