Python 3home |
Most Python scripts are not run from an IDE.
Developers hang out on the command line for two important reasons:
Your OS doesn't necessarily know where Python is until we tell it.
We should begin by seeing whether our OS already knows where python is; even if it does, we should learn the procedure for telling it where it is (for those future situations where it doesn't yet know): Windows
If you don't see a path, please see the alternative approach, below. Mac
An alternative approach to determine the path to Python is to run this program from your IDE:
import sys
print(sys.executable)
argparse allows us to collect and validate arguments
Many programs use command line arguments that follow the following pattern:
python myprog.py --year 2023 --factor SMB --flavors cherry vanilla chocolate
The program may require certain arguments, and show an error if they aren't passed:
python myprog.py --year 2023 usage: myprog.py [-h] -y YEAR -f FACTOR ProgramName: error: the following arguments are required: -y/--year, -f/--factor
Such programs also offer help messages when the user typs --help as an argument:
python myprog.py --help usage: test.py [-h] -y YEAR -f FACTOR Reads Fama-French file to show sum of values for a selected year and selected factor optional arguments: -h, --help show this help message and exit -y YEAR, --year YEAR -f FACTOR, --factor FACTOR
This sets up our program to use more sophisticated command line arguments. We will add the argument options in the next step.
import argparse
import os
import sys
parser = argparse.ArgumentParser(
prog=os.path.basename(sys.argv[0]),
description='Reads Fama-French file to show sum of values for a selected year and selected factor')
All of the above arguments are optional, but if used will show up in a help message if the user passes the command line argument '-h' or '--help'
Here's how we add arguments. The optional required=True causes argparse to stop the program if the argument was not passed.
parser.add_argument('-y', '--year', required=True)
parser.add_argument('-f', '--factor', required=True, choices=['Mkt-RF', 'SMB', 'HML', 'RF'])
parser.add_argument('-v', '--flavors', nargs='+') # allow 1 or more values
args = parser.parse_args()
print(args.year) # retrieve the value passed as <B>--year</B>
print(args.factor) # retrieve the value passed as <B>--factor</B>
print(args.flavors) # retrieve list of values passed as <B>--flavors</B>
Once all arguments have been added, parser.parse_args() returns an object whose attributes match the argument name. See the documentation for more options to .add_argument() that can:
On Windows we use the GUI; on Mac we must edit a "hidden" file.
Windows
Mac
sys.argv is a list that holds string arguments entered at the command line
sys.argv example
a python script myscript.py
import sys # import the 'system' library
print('first arg: ' + sys.argv[1]) # print first command line arg
print('second arg: ' + sys.argv[2]) # print second command line arg
running the script from the command line
$ python myscript.py hello there first arg: hello second arg: there
sys.argv is a list that is automatically provided by the sys module. It contains any string arguments to the program that were entered at the command line by the user. If the user does not type arguments at the command line, then they will not be added to the sys.argv list. sys.argv[0] sys.argv[0] always contains the name of the program itself Even if no arguments are passed at the command line, sys.argv always holds one value: a string containing the program name (or more precisely, the pathname used to invoke the script). example runs
a python script myscript2.py
import sys # import the 'system' library
print(sys.argv)
running the script from the command line (passing 3 arguments)
$ python myscript2.py hello there budgie ['myscript2.py', 'hello', 'there', 'budgie']
running the script from the command line (passing no arguments)
$ python myscript2.py ['myscript2.py']
Special note on debugging: command-line arguments can't be used when running the PyCharm debugger. Instead, you can hard-code your arguments thusly:
import sys
sys.argv = ['progname.py', 'shakespeare/', 'support']
Now you won't need to enter arguments on the command line as you have re-established them using the sys.argv variable.
An IndexError occurs when we ask for a list index that doesn't exist. If we try to read sys.argv, Python can raise this error if the arg is not passed by the user.
a python script addtwo.py
import sys # import the 'system' library
firstint = int(sys.argv[1])
secondint = int(sys.argv[2])
mysum = firstint + secondint
print(f'the sum of the two values is {mysum}')
running the script from the command line (passing 2 arguments)
$ python addtwo.py 5 10 the sum of the two values is 15
exception! running the script from the command line (passing no arguments)
$ python addtwo.py Traceback (most recent call last): File "addtwo.py", line 3, in <module> firstint = int(sys.argv[1]) IndexError: list index out of range
The above error occurred because the program asks for items at subscripts sys.argv[1] and sys.argv[2], but because no elements existed at those indices, Python raised an IndexError exception. How to handle this exception? Test the len() of sys.argv, or trap the exception.