Passing arguments from the command line
If you've read the last article from this series, you might probably be thinking "What if I have some configuration that I need to change at every run? I DO NOT want to keep changing the files each time!!". Yes, and I hear you. Sometimes, you might need to change a few configurations each time. While creating configuration files makes our life easier by eliminating the need to change the code itself, we still have to change the config file itself.
You might find it inconvenient when you are experimenting with your code and you need to change the config file every time. In the last example, we supplied database credentials to the config file. While it solved the problem of having to edit the code, we still risk the exposure of sensitive information. I hope you understand where I am going with this. Yes, you can also pass configuration when you execute the scripts from the command line and today, I will discuss ways you can do that.
sys.argv
I don't know why, but I always used to get nauseated seeing this. I stayed far away from it as much as possible. But, this will save you a lot of time when writing smaller scripts and experimenting with your code. Of course, you can also use it for some of your bigger projects if you want.
Most of us, when we started with Python, we probably used some IDEs or code editors (like vs code, PyCharm, and Jupyter Notebooks). It took quite a while for me to realize that to run a Python program, I didn't need a run button on my editor. I could run a Python program like this:
python main.py
Let's try to edit main.py
and print what's inside sys.argv
.
#main.py file
import sys
print(sys.argv)
When we run the main.py
file, we get:
python main.py
>>>['main.py']
Did you get where we are going with this? Before I spoil it for you, let's try something else.
python main.py preprocess
>>>['main.py', 'preprocess']
I hope you are seeing it as well. sys.argv
is a list of strings, where the elements of the list are the strings passed in the command line. You can pass as many parameters this way and it can be accessed by indexing the relevant element. Let's see an example where you would like to pass some credentials.
#main.py file
import sys
username = sys.argv[1]
password = sys.argv[2]
print(f"{username} and {password}")
python main.py username password
>>> username and password
One more limitation of using this all the parameters are treated as strings and it becomes complicated and more confusing if we wanted to pass strings.
argparse
As you soon start to notice, we can only access the passed configuration by indexing. If you have a long list of parameters to be passed, it will soon get confusing as it is easy to lose track. It's almost as if it would have been a lot better if we could name these parameters. argparse
does exactly this!
python main.py -u username -p password
python main.py --user username --pass password
You must have encountered something like this used to change the configuration of the Python program. Let's see how to do this.
#main.py file
import argparse
parser = argparse.ArgumentParser(description="Process username and password")
parser.add_argument("-u", "--user", type=str, help="Username")
parser.add_argument("-p", "--pass", type=str, help="Password")
args = parser.parse_args()
print(f"Username: {args.username}")
print(f"Password: {args.password}")
python main.py -u username -p password
>>>Username: username
>>>Password: password
Let's also see how we can now pass lists.
#main.py file
import argparse
parser = argparse.ArgumentParser(description="Lists")
parser.add_argument("-n", "--numbers", type=int, nargs="+", help="Array of numbers")
parser.add_argument("-s", "--strings", type=str, nargs="+", help="Array of strings")
args = parser.parse_args()
print("Numbers:", args.numbers)
print("Strings:", args.strings)
python main.py -n 1 2 3 4 5 -s apple banana cherry
>>>Numbers: [1, 2, 3, 4, 5]
>>>Strings: ['apple', 'banana', 'cherry']
This way you can pass various types of named parameters as configuration for your Python program.
Additionally, you can get help using the script.
python main.py --help
usage: python.py [-h] [-n NUMBERS [NUMBERS ...]] [-s STRINGS [STRINGS ...]]
Lists
optional arguments:
-h, --help show this help message and exit
-n NUMBERS [NUMBERS ...], --numbers NUMBERS [NUMBERS ...]
Array of numbers
-s STRINGS [STRINGS ...], --strings STRINGS [STRINGS ...]
Array of strings