How do I run Python script using arguments in windows command line How do I run Python script using arguments in windows command line windows windows

How do I run Python script using arguments in windows command line


  • 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 sysdef hello(a,b):    print "hello and that's your sum:", a + bif __name__ == "__main__":    a = int(sys.argv[1])    b = int(sys.argv[2])    hello(a, b)


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.


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 sysdef hello(a, b):    print "hello and that's your sum:"    sum = a+b    print sumif __name__== "__main__":    hello(int(sys.argv[1]), int(sys.argv[2]))

Then run the code with this command:

python hello.py 1 1