Außerhalb des IPython Notebooks wird direkt in Textdateien mit der Endung ".py
" programmiert.
Diese Dateien verhalten sich wie große Zellen, die vom Python Interpreter vollständig ausgewertet werden.
Darauf aufbauend, kann das einzelne Programm andere Python Dateien laden (mittels import
) bzw. eine Sammlung von Python Dateien kann ein vollständiges Python Modul bilden.
Im folgenden das wichtigste, wie man eine einzelne Python Datei erzeugt:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
angegeben."""
oder mit vorangestellten #
, die erklären was das Programm ist, was es machen sollte, wer es Programmiert hat, unter welcher Lizenz es steht, etc.import
-Statements beginnt und dann Funktionen und Klassen definiert.if __name__ == "__main__":
...
...
Jetzt ein konkretes Beispiel zur besseren Veranschaulichung:
%%writefile res/program1.py
#!/usr/bin/env python
# -*- coding: utf8 -*-
# This is an example, which contains all the main parts for a proper standalone program
# Copyright: Harald Schilly <harald.schilly@univie.ac.at>, 2014
# License: Apache 2.0
import math
def calculate(x):
"""
The important calculation
"""
if x < 0:
y = - x
else:
y = math.sqrt(x)
return x + y
if __name__ == "__main__":
z = 21
print("Program started, z = %f" % z)
result = calculate(z)
print("Result = %f" % result)
Overwriting res/program1.py
Die vorherige Zelle speichert beim Ausführen durch "%%writefile
" den Inhalt in der angegebenen Datei ("res/program1.py
") ab.
Nun führen wir das Programm mit dem Python-Interpreter aus (Das !
schickt den angegebenen Befehl direkt an die Kommandozeile)
!python res/program1.py
Failed to import the site module Traceback (most recent call last): File "/projects/anaconda3/lib/python3.5/site.py", line 72, in <module> import os File "/projects/anaconda3/lib/python3.5/os.py", line 666, in <module> from _collections_abc import MutableMapping File "/projects/anaconda3/lib/python3.5/_collections_abc.py", line 56 async def _coro(): pass ^ SyntaxError: invalid syntax
Programme haben üblicherweise Eingaben, unter anderem in der Form von Kommandozeilenargumenten.
Das sind einzelne Zeichenketten, die dem Programmaufruf in der Kommandozeile nachgestellt werden.
Diese Strings sind dann in sys.args
vom Programm aus abrufbar.
Beispiel:
%%writefile res/program2.py
#!/usr/bin/env python
if __name__ == "__main__":
import sys
for idx, arg in enumerate(sys.argv):
print("Argument %2d: %s" % (idx, arg))
Overwriting res/program2.py
!python3 res/program2.py xx argument2 "with a space"
Failed to import the site module Traceback (most recent call last): File "/projects/anaconda3/lib/python3.5/site.py", line 72, in <module> import os File "/projects/anaconda3/lib/python3.5/os.py", line 666, in <module> from _collections_abc import MutableMapping File "/projects/anaconda3/lib/python3.5/_collections_abc.py", line 56 async def _coro(): pass ^ SyntaxError: invalid syntax
Es ist neben dieser einfachen Art der Parameterübergabe auch eine komplexere Art möglich.
Oft will man nur ausgewählte Parameter übergeben.
Die geschieht mit einem speziellen Syntax (vorangestellte "-
" und "--
"),
welcher zwischen boolschen Flags und Variablen unterscheidet,
durch das Argparse modul.
Beispiel:
$ python program3.py 21 --negate --y=21
%%writefile res/program3.py
#!/usr/bin/env python
def parse_args():
import argparse
parser = argparse.ArgumentParser()
# mandatory x value, no prefix
parser.add_argument("x", type=int, help="The x value")
# optional y value
parser.add_argument("--y", type=int, help="The optional y value", default=42)
# "negate"-flag
parser.add_argument("--negate", action="store_true", help="Should x be negated?")
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
print("Arguments: %s" % args)
x = args.x
if args.negate:
x = -x
result = x + args.y
print("Result: %f" % result)
Overwriting res/program3.py
!python res/program3.py 21
File "/projects/anaconda3/lib/python3.5/site.py", line 176 file=sys.stderr) ^ SyntaxError: invalid syntax
!python res/program3.py 21 --negate
File "/projects/anaconda3/lib/python3.5/site.py", line 176 file=sys.stderr) ^ SyntaxError: invalid syntax
!python res/program3.py 10 --y=0
File "/projects/anaconda3/lib/python3.5/site.py", line 176 file=sys.stderr) ^ SyntaxError: invalid syntax
Und es gibt außerdem eine Hilfe, und eine implizite Kontrolle ob die Argumente korrekt übergeben wurden:
!python res/program3.py --y=0
File "/projects/anaconda3/lib/python3.5/site.py", line 176 file=sys.stderr) ^ SyntaxError: invalid syntax
!python res/program3.py 70 -ngate
File "/projects/anaconda3/lib/python3.5/site.py", line 176 file=sys.stderr) ^ SyntaxError: invalid syntax
!python res/program3.py --help
File "/projects/anaconda3/lib/python3.5/site.py", line 176 file=sys.stderr) ^ SyntaxError: invalid syntax