65

This is my python hello.py script:

def hello(a,b):
    print "hello and that's your sum:"
    sum=a+b
    print sum
    import sys

if __name__ == "__main__":
    hello(sys.argv[2])

The problem is that it can't be run from the windows command line prompt, I used this command:

C:\Python27>hello 1 1

But it didn't work unfortunately, may somebody please help?

falsetru
  • 314,667
  • 49
  • 610
  • 551
user2563817
  • 703
  • 1
  • 5
  • 3
  • 2
    Please use proper code formatting – thibauts Jul 09 '13 at 09:12
  • If "hello.py" is in a `PATH` directory, and running `hello 1 1` doesn't pass the command-line arguments, then the .py file association is broken. If CMD or PowerShell doesn't find "hello.py", then .PY isn't in `PATHEXT`. You should not need to run `python hello.py 1 1`. That's annoying since it requires using a qualified path for hello.py or changing to its directory first. – Eryk Sun Jul 30 '17 at 10:00

7 Answers7

63
  • import sys out of hello function.
  • arguments should be converted to int.
  • String literal that contain ' should be escaped or should be surrouned by ".
  • Did you invoke the program with python hello.py <some-number> <some-number> in command line?

import sys

def hello(a,b):
    print "hello and that's your sum:", a + b

if __name__ == "__main__":
    a = int(sys.argv[1])
    b = int(sys.argv[2])
    hello(a, b)
falsetru
  • 314,667
  • 49
  • 610
  • 551
  • 3
    `import` not in top-level - it's not recommended. Should be placed before `def hello` – ElmoVanKielmo Jul 09 '13 at 09:52
  • 1
    Using `print` without parentheses is not advisable in Python 2.7. You should aim for compatibility with Python 3 ;) – Agostino Apr 06 '15 at 20:04
  • @Agostino, `print('a', 'b')` will print `('a', 'b')` in Python 2.x, unlike Python 3.x which print `a b`. (unless you are using `from __future__ import print_function` in Python 2.x) – falsetru Apr 07 '15 at 00:12
  • It's not difficult to find a way to make it work for both. E.g. `print("hello and that's your sum: " + str(a + b))` – Agostino Apr 07 '15 at 00:30
  • @Agostino, I would use `from __future__ import print_function` as I mentioned in previous comment if I really want to use `print` as a function :) or `print("hello and that's your sum: %s" % (a+b))` or `print("hello and that's your sum: {}".format(a+b))` – falsetru Apr 07 '15 at 01:30
  • With the solution I proposed, you don't need to import anything from `__future__`. In Python 2.7 you *can* use parentheses with `print`. As long as you print a single string and use `+` to concatenate substrings, you get the same behavior. – Agostino Apr 07 '15 at 16:29
  • @Agostino, Same for string formatting operator (%), or `str.format`. – falsetru Apr 07 '15 at 23:00
  • What about the int cast? – Agostino Apr 08 '15 at 15:26
  • @Agostino, Without `int`, `sys.argv[...]` are strings. – falsetru Apr 08 '15 at 22:27
13

To execute your program from the command line, you have to call the python interpreter, like this :

C:\Python27>python hello.py 1 1

If you code resides in another directory, you will have to set the python binary path in your PATH environment variable, to be able to run it, too. You can find detailed instructions here.

Community
  • 1
  • 1
thibauts
  • 1,618
  • 8
  • 8
13

Here are all of the previous answers summarized:

  • modules should be imported outside of functions.
  • hello(sys.argv[2]) needs to be indented since it is inside an if statement.
  • hello has 2 arguments so you need to call 2 arguments.
  • as far as calling the function from terminal, you need to call python .py ...

The code should look like this:

import sys
def hello(a, b):
    print "hello and that's your sum:"
    sum = a+b
    print sum

if __name__== "__main__":
    hello(int(sys.argv[1]), int(sys.argv[2]))

Then run the code with this command:

python hello.py 1 1
Charles
  • 817
  • 1
  • 13
  • 36
C. Glass
  • 141
  • 1
  • 3
8

I found this thread looking for information about dealing with parameters; this easy guide was so cool:

import argparse

parser = argparse.ArgumentParser(description='Script so useful.')
parser.add_argument("--opt1", type=int, default=1)
parser.add_argument("--opt2")

args = parser.parse_args()

opt1_value = args.opt1
opt2_value = args.opt2

run like:

python myScript.py --opt2 = 'hi'
hestellezg
  • 1,647
  • 25
  • 28
  • 1
    Thanks for sharing this with the reference where well summaries the most common usage of py arguments. – seedme Feb 17 '21 at 04:41
4

Your indentation is broken. This should fix it:

import sys

def hello(a,b):
    print 'hello and thats your sum:'
    sum=a+b
    print sum

if __name__ == "__main__":
    hello(sys.argv[1], sys.argv[2])

Obviously, if you put the if __name__ statement inside the function, it will only ever be evaluated if you run that function. The problem is: the point of said statement is to run the function in the first place.

Nils Werner
  • 28,291
  • 6
  • 62
  • 82
  • thank you so much for this code,it worked,but I'd like to know what really was wrong, and where to find more details about it please,thank you so much – user2563817 Jul 09 '13 at 11:24
1
import sys

def hello(a, b):
    print  'hello and that\'s your sum: {0}'.format(a + b)

if __name__ == '__main__':
    hello(int(sys.argv[1]), int(sys.argv[2]))

Moreover see @thibauts answer about how to call python script.

ElmoVanKielmo
  • 9,272
  • 2
  • 28
  • 41
0

There are more than a couple of mistakes in the code.

  1. 'import sys' line should be outside the functions as the function is itself being called using arguments fetched using sys functions.
  2. If you want correct sum, you should cast the arguments (strings) into floats. Change the sum line to --> sum = float(a) + float(b).
  3. Since you have not defined any default values for any of the function arguments, it is necessary to pass both arguments while calling the function --> hello(sys.argv[2], sys.argv[2])

    import sys def hello(a,b): print ("hello and that's your sum:") sum=float(a)+float(b) print (sum)

    if __name__ == "__main__": hello(sys.argv[1], sys.argv[2])

Also, using "C:\Python27>hello 1 1" to run the code looks fine but you have to make sure that the file is in one of the directories that Python knows about (PATH env variable). So, please use the full path to validate the code. Something like:

C:\Python34>python C:\Users\pranayk\Desktop\hello.py 1 1