seesee
seesee

Reputation: 1155

Passing variables at runtime

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.

Upvotes: 2

Views: 31699

Answers (6)

Adem Öztaş
Adem Öztaş

Reputation: 21446

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

Upvotes: 16

icedwater
icedwater

Reputation: 4887

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...

Upvotes: 3

TerryA
TerryA

Reputation: 59984

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

Upvotes: 0

user2286078
user2286078

Reputation:

You can do this:

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

answer = n1 + n2
print answer

Upvotes: 1

lulyon
lulyon

Reputation: 7225

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

import sys 
print int(sys.argv[1]) + int(sys.argv[2])

Upvotes: 1

Sam Nicholls
Sam Nicholls

Reputation: 899

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.

Upvotes: 2

Related Questions