Pass Arguments To a Python Script

In many use cases, your might need to develop or build a Python script that accepts arguments as values without prompting from them.


For the above reason, Python allows us to write scripts and programs that accept values from the command-line either when a user runs the script or another application.


In the below code example, I have a small script that accepts two arguments (3 if you counts the script name) and prints the values to the screen.

Note: The script is the actual script name that we pass to Python and it is included in the argument count which is actually 3, not two. The script is always the first argument.

The script is using the sys library argv submodule for the argument function.

from sys import argv
script, myvalue1, myvalue2  = argv
print (f"\n This is value number #1 {myvalue1} \n This is value number #2 {myvalue2}")


To run the script, I will use the following command.

python3 Develop Runtime

The expected output is showing below.

 This is value number #1 Develop 
 This is value number #2 Runtime

1 comment

Leave a comment

Your email address will not be published.