This section covers the bits and pieces of Python programming, commandline arguments, executing a Python program, distributing Python applications.
The various operating systems means that Python programs run differently on each depending on what you use to run them, linux and windows commandline run the same, using IDE's they may run different as the IDE's generally add there own synax to the commandline.
| Python commandline | python script1.py
Note: this is presuming that the python executable is in your path |
| Commandline arguments | #### script1.py ####
import sys
print("Script Name: " + sys.argv[0]) # note script name is first argument
print("Argument 1: " + sys.argv[1]) # access specific argument
for arg in sys.argv: # loop through the arguments that are stored in the sys.argv list
print(arg)
#### Run the script passing in the script name and 3 arguments
python script1.py arg1 arg2 arg3
Note: a list is created called sys.argv and you can access the elements as a normal list |
You can redirect basic standard in/out of a script
| Python commandline | #### script.py ####
import sys
def main():
contents = sys.stdin.read()
sys.stdout.write(contents.replace(sys.argv[1], sys.argv[2]))
main()
### run this command as per below, make sure infile exist with something in it
python script.py arg1 arg2 arg3 arg4 < infile > outfile
Note: < = read, > = write (over writing file) and >> = append to file
|
Below are some useful modules regarding the commandline, there are many thousands of modules if you go to pypi
| argparse module | from argparse import ArgumentParser
def main():
parser = ArgumentParser()
parser.add_argument("indent", type=int, help="indent for report")
parser.add_argument("input_file", help="read data from this file")
parser.add_argument("-f", "--file", dest="filename", help="write report to FILE", metavar="FILE")
parser.add_argument("-x", "--xray",help="specify xray strength factor")
parser.add_argument("-q", "--quiet", action="store_false", dest="verbose", default=True, help="don't print status messages to stdout")
args = parser.parse_args()
print("arguments:", args)
main()
## Running the script
python opts.py -x100 -q -f outfile 2 arg2 |
| fileinput module | import fileinput
def main():
for line in fileinput.input():
if not line.startswith('##'):
print(line, end="")
main()
## Running the script
python script4.py sole1.tst sole2.tst
|
When ever you import a module all any code statements are executed, there are times when you might not want this to happen, when a script runs the __name__ becomes __main__, thus you know that this is the running script, if you import a script the __name__ becomes the script name, you can use a conditonal to make sure that code does not run when importing
| argparse module | #### progModule1.py
def progmodule2():
print("This is method progmodule2")
# if running directly __name__ will be __main__
# if importing from another script __name__ will be progModule1
print(__name__)
# if you run directly the progmodule2() function will be called
# if you import from another script progmodule2() will NOT be called
if __name__ == '__main__':
progmodule2() # this will only be called if running directly
#### progModule2.py
import progModule1 as progModule2 # any statements in progModule2 will be executed
def main():
print("This is method main")
# if running directly __name__ will be __main__
# if importing from another script __name__ will be progModule2
print("This is progModule " + __name__)
main()
|
Distributing Python Applications
There are a number of ways to distribute Python applications
I will return to this section once I have some examples of how to create distributed packages.