2

I wish to pass in some variables into python during run time

python add2values.py 123 124

then in the python script it will take those 2 values and add together.

OR

python add2values.py a=123 b=124

then in the python script it will take those 2 values and add together.

seesee
  • 1,145
  • 5
  • 20
  • 46

6 Answers6

16

You can use sys.argv

test.py

#!/usr/bin/env python

import sys
total = int(sys.argv[1]) +  int(sys.argv[2])
print('Argument List: %s' % str(sys.argv))
print('Total : %d' % total)  

Run the following command:

$ python test.py 123 124
Argument List: ['test.py', 'arg1', 'arg2', 'arg3']
Total : 247
Adem Öztaş
  • 20,457
  • 4
  • 34
  • 42
3

There are a few ways to handle command-line arguments.

One is, as has been suggested, sys.argv: an array of strings from the arguments at command line. Use this if you want to perform arbitrary operations on different kinds of arguments. You can cast the first two arguments into integers and print their sum with the code below:

import sys
n1 = sys.argv[1]
n2 = sys.argv[2]

print (int(n1) + int(n2))

Of course, this does not check whether the user has input strings or lists or integers and gives the risk of a TypeError. However, for a range of command line arguments, this is probably your best bet - to manually take care of each case.

If your script/program has fixed arguments and you would like to have more flexibility (short options, long options, help texts) then it is worth checking out the optparse and argparse (requires Python 2.7 or later) modules. Below are some snippets of code involving these two modules taken from actual questions on this site.

import argparse

parser = argparse.ArgumentParser(description='my_program')
parser.add_argument('-verbosity', help='Verbosity', required=True)

optparse, usable with earlier versions of Python, has similar syntax:

from optparse import OptionParser

parser = OptionParser()
...
parser.add_option("-m", "--month", type="int",
              help="Numeric value of the month", 
              dest="mon")

And there is even getopt if you prefer C-like syntax...

Community
  • 1
  • 1
icedwater
  • 4,701
  • 3
  • 35
  • 50
2

The argparse module allows you to write simple command line interfaces without much effort and is very flexible. It'll handle things like checking the input variables are of the right type that you specify and just make it easier to get on with writing your program.

The first example even demonstrates a similar program that will accept a list of strings to sum.

Sam Nicholls
  • 861
  • 4
  • 16
1

You can do this:

import sys
n1 = int(sys.argv[1])
n2 = int(sys.argv[2])

answer = n1 + n2
print answer
1

I think you mean passing parameter through command line argument. The code:

import sys 
print int(sys.argv[1]) + int(sys.argv[2])
lulyon
  • 6,707
  • 7
  • 32
  • 49
0

Use the sys module. This will add any arguments given:

import sys
print sum(map(int, sys.argv[1:]))

map() will have to be used because all elements in sys.argv are strings. We also have to slice the list because the first element will be the script name.

When run:

$ python myfile.py 123 124
247
$ python myfile.py 1 2 3
6
TerryA
  • 58,805
  • 11
  • 114
  • 143