This worked for me:

import sys
firstarg=sys.argv[1]
secondarg=sys.argv[2]
thirdarg=sys.argv[3]
Answer from Peter Gerhat on Stack Exchange
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ python_command_line_arguments.htm
Python - Command-Line Arguments
Python's sys module provides access to any command-line arguments via the sys.argv variable. sys.argv is the list of command-line arguments and sys.argv[0] is the program i.e. the script name.
Discussions

Pass arguments from cmd to python script - Stack Overflow
I write my scripts in python and run them with cmd by typing in: C:\> python script.py Some of my scripts contain separate algorithms and methods which are called based on a flag. Now I would l... More on stackoverflow.com
๐ŸŒ stackoverflow.com
command line - Python file keyword argument? - Stack Overflow
However, I would like to send keyword arguments to a python script, and retrieve them as a dictionary: More on stackoverflow.com
๐ŸŒ stackoverflow.com
Passing argument to function from command line
My son wrote a command line program in ruby for me. I want to convert it to python. To run the program he has done this on the command line; โ€œ./myProgram.rb โ€˜argumentโ€™โ€. Then the 'argument โ€™ is passed into the program to be processed. I want to do the same in python. More on discuss.python.org
๐ŸŒ discuss.python.org
8
0
June 25, 2025
Argument passing in python script
Hey, Iโ€™m working with the invoke python method in UIPaths I want to pass 3 arg to python fuction 2 string and one python obj how can I do that?? More on forum.uipath.com
๐ŸŒ forum.uipath.com
14
0
September 22, 2023
Top answer
1 of 3
139

You can use the Optional Arguments like so.

With this program:

#!/usr/bin/env python3

import argparse, sys

parser=argparse.ArgumentParser()

parser.add_argument("--bar", help="Do the bar option")
parser.add_argument("--foo", help="Foo the program")

args=parser.parse_args()

print(f"Args: {args}\nCommand Line: {sys.argv}\nfoo: {args.foo}")
print(f"Dict format: {vars(args)}")

Make it executable:

$ chmod +x prog.py

Then if you call it with:

$ ./prog.py --bar=bar-val --foo foo-val

It prints:

Args: Namespace(bar='bar-val', foo='foo-val')
Command Line: ['./prog.py', '--bar=bar-val', '--foo', 'foo-val']
foo: foo-val
Dict format: {'bar': 'bar-val', 'foo': 'foo-val'}

Or, if the user wants help argparse builds that too:

 $ ./prog.py -h
usage: prog.py [-h] [--bar BAR] [--foo FOO]

options:
  -h, --help  show this help message and exit
  --bar BAR   Do the bar option
  --foo FOO   Foo the program

2022-08-30: Updated to Python3 this answer...

2 of 3
43

The answer is yes. A quick look at the argparse documentation would have answered as well.

Here is a very simple example, argparse is able to handle far more specific needs.

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--foo', '-f', help="a random options", type= str)
parser.add_argument('--bar', '-b', help="a more random option", type= int, default= 0)

print(parser.format_help())
# usage: test_args_4.py [-h] [--foo FOO] [--bar BAR]
# 
# optional arguments:
#   -h, --help         show this help message and exit
#   --foo FOO, -f FOO  a random options
#   --bar BAR, -b BAR  a more random option

args = parser.parse_args("--foo pouet".split())
print(args)  # Namespace(bar=0, foo='pouet')
print(args.foo) # pouet
print(args.bar) # 0

Off course, in a real script, you won't hard-code the command-line options and will call parser.parse_args() (without argument) instead. It will make argparse take the sys.args list as command-line arguments.

You will be able to call this script this way:

test_args_4.py -h  # prints the help message
test_args_4.py -f pouet  # foo="pouet", bar=0 (default value)
test_args_4.py -b 42  # foo=None, bar=42
test_args_4.py -b 77 -f knock  # foo="knock", bar=77

You will discover a lot of other features by reading the doc ;)

๐ŸŒ
OpenSourceOptions
opensourceoptions.com โ€บ how-to-pass-arguments-to-a-python-script-from-the-command-line
How to Pass Arguments to a Python Script from the Command Line โ€“ OpenSourceOptions
Now call getopt.getopt and pass it the arguments from the command line, but not the script name (like this: argv[1:]). In the call to getopt is also where we specify both the parameter short and long names. The colons (:) following i, u, and o indicate that a value is required for that parameter.
๐ŸŒ
Medium
moez-62905.medium.com โ€บ the-ultimate-guide-to-command-line-arguments-in-python-scripts-61c49c90e0b3
The Ultimate Guide to Command Line Arguments in Python Scripts | by Moez Ali | Medium
April 18, 2023 - In Python, command-line arguments are accessed through the sys.argv list. The first item in the list is always the name of the script itself, and subsequent items are the arguments passed to the script.
Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ @evaGachirwa โ€บ running-python-script-with-arguments-in-the-command-line-93dfa5f10eff
Running Python script with Arguments in the command line | by Eva Mwangi | Medium
April 22, 2023 - Through sys.argv arguments are passed from the command line to the Python script. For example, add this code to a test.py file: import sys def get_sum_of_num(num1,num2,num3): return(int(num1)+int(num2)+int(num3)) if __name__ == "__main__": num1 = sys.argv[1] num2 = sys.argv[2] num3 = sys.argv[3] print(get_sum_of_num(num1, num2, num3)) Running the script on cmd ยท
๐ŸŒ
Thomas Stringer
trstringer.com โ€บ python-named-arguments
Why You Should Typically Use Named Arguments in Python | Thomas Stringer
December 27, 2020 - When invoking functions in Python, you can usually pass your arguments either by position or name. There are advantages (and disadvantages) to each approach.
๐ŸŒ
MachineLearningMastery
machinelearningmastery.com โ€บ home โ€บ blog โ€บ command line arguments for your python script
Command Line Arguments for Your Python Script - MachineLearningMastery.com
June 21, 2022 - The following script allows us to pass in values from the command line into Python: We save these few lines into a file and run it in command line with an argument: ... Then, you will see it takes our argument, converts it into an integer, adds one to it, and prints. The list sys.argv contains the name of our script and all the arguments (all strings), which in the above case, is ["commandline.py", "15"].
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ command-line-arguments-in-python
Command Line Arguments in Python - GeeksforGeeks
The simplest way to access command line arguments in Python is through the sys module. It provides a list called sys.argv that stores everything you type after python in the command line. sys.argv: A list in Python that contains all the command-line ...
Published ย  December 17, 2025
๐ŸŒ
Real Python
realpython.com โ€บ python-command-line-arguments
Python Command-Line Arguments โ€“ Real Python
August 27, 2023 - You can compile the code above on Linux with gcc -o main main.c, then execute with ./main to obtain the following: ... Unless explicitly expressed at the command line with the option -o, a.out is the default name of the executable generated by the gcc compiler. It stands for assembler output and is reminiscent of the executables that were generated on older UNIX systems. Observe that the name of the executable ./main is the sole argument. Letโ€™s spice up this example by passing a few Python command-line arguments to the same program:
๐ŸŒ
Analytics Vidhya
analyticsvidhya.com โ€บ home โ€บ 3 easy ways to handle command line arguments in python
3 Easy Ways to Handle Command Line Arguments in Python
January 15, 2025 - The simplest way to handle command line arguments is by using the sys moduleโ€™s argv. The argv is a list in Python, which contains the command-line-arguments pass argument to python script.
๐ŸŒ
Medium
medium.com โ€บ @BetterEverything โ€บ run-python-script-from-command-line-with-keyword-arguments-9c78cafd0e05
Run Python Script from Command Line with Keyword Arguments | by Better Everything | Medium
August 13, 2024 - Run Python Script from Command Line with Keyword Arguments Learn to pass keyword arguments โ€” aka named arguments โ€” to a Python file when running it from command line. Passing arguments and โ€ฆ
๐ŸŒ
Trey Hunner
treyhunner.com โ€บ 2018 โ€บ 04 โ€บ keyword-arguments-in-python
Keyword (Named) Arguments in Python: How to Use Them
And remember that you can accept arbitrary keyword arguments to the functions you define and pass arbitrary keyword arguments to the functions you call by using the ** operator. Important objects deserve names and you can use keyword arguments ...
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Passing argument to function from command line - Python Help - Discussions on Python.org
June 25, 2025 - My son wrote a command line program in ruby for me. I want to convert it to python. To run the program he has done this on the command line; โ€œ./myProgram.rb โ€˜argumentโ€™โ€. Then the 'argument โ€™ is passed into the prograโ€ฆ
๐ŸŒ
Python
docs.python.org โ€บ 3.3 โ€บ library โ€บ argparse.html
16.4. argparse โ€” Parser for command-line options, arguments and sub-commands โ€” Python 3.3.7 documentation
All parameters should be passed as keyword arguments. Each parameter has its own more detailed description below, but in short they are: prog - The name of the program (default: sys.argv[0]) usage - The string describing the program usage (default: generated from arguments added to parser)
๐ŸŒ
UiPath Community
forum.uipath.com โ€บ help โ€บ activities
Argument passing in python script - Activities - UiPath Community Forum
Hey, Iโ€™m working with the invoke python method in UIPaths I want to pass 3 arg to python fuction 2 string and one python obj how can I do that??
Published ย  September 22, 2023
๐ŸŒ
Medium
datageeks.medium.com โ€บ passing-parameters-to-the-python-script-from-the-command-line-139a9fc94ee
Passing arguments to the Python script from the command line. | by DataGeeks | Medium
December 26, 2022 - Run the script again from the command line. bash-3.2$ python3 python_arguments.py 'my' 'name' 'is' 'khushboo' ----------output-------------------